如果没有使用文本或JSon文件的按钮,则无法在活动开始时获得Android语音朗读功能

问题描述 投票:1回答:1

这是我想做的,我想让TTS在活动开始前开始对话。我找到了大量文章,内容涉及如何在EditText和按钮中执行与用户输入类似的操作,但是我想让TTS从文本文件(最好是Json文件)中读取,我不确定我丢失了什么,我记不起我不是程序员,而是从许多不同的文章中拼凑而成的。

TextToSpeech myTTS;
InputStream stream;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_aibutton);

    myTTS=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitActivity() {
        @Override
        public void onInit(int status) {
            if(status != TextToSpeech.ERROR) {
                myTTS.setLanguage(Locale.US);
            }
        }
    });

    stream.setOnActivityListener(new Stream.OnActivityListener() {
        @Override
        public void OnInitActivity(View v) {
            String toSpeak = stream.getText().toString();
            InputStream stream = this.getResources().openRawResource(R.raw.stringquestions);
            Toast.makeText(getApplicationContext(), toSpeak,Toast.LENGTH_SHORT).show();
            myTTS.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
        }
    });
}

public void onPause(){
    if(myTTS !=null){
        myTTS.stop();
        myTTS.shutdown();
    }
    super.onPause();
}

// act on result of TTS data check
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == MY_DATA_CHECK_CODE) {
        if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
            // the user has the necessary data - create the TTS
            myTTS = new TextToSpeech(this, this);
        } else {
            // no data - install it now
            Intent installTTSIntent = new Intent();
            installTTSIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
            startActivity(installTTSIntent);
        }
    }
}

// setup TTS
public void onInit(int initStatus) {

    // check for successful instantiation
    if (initStatus == TextToSpeech.SUCCESS) {
        if (myTTS.isLanguageAvailable(Locale.US) == TextToSpeech.LANG_AVAILABLE)
            myTTS.setLanguage(Locale.US);
    } else if (initStatus == TextToSpeech.ERROR) {
        Toast.makeText(this, "Sorry! Text To Speech failed...",
                Toast.LENGTH_LONG).show();
    }
}
java android json text-to-speech
1个回答
0
投票
 Move the tts speak code inside onInit code. 
 The tts speak method works only after onInit has been called.

 myTTS=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitActivity() {
    @Override
    public void onInit(int status) {
        if(status != TextToSpeech.ERROR) {
            myTTS.setLanguage(Locale.US);
            stream.setOnActivityListener(new Stream.OnActivityListener() {
               @Override
               public void OnInitActivity(View v) {
                  String toSpeak = stream.getText().toString();
                  InputStream stream = 
                    this.getResources().openRawResource(R.raw.stringquestions);
                  Toast.makeText(getApplicationContext(), toSpeak,Toast.LENGTH_SHORT).show();
                  myTTS.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
    }
});
        }
    }
});
© www.soinside.com 2019 - 2024. All rights reserved.