简体   繁体   中英

Text to Speech android not working

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game);
    init();
}

public void init() {
    tts = new TextToSpeech(Game.this, new OnInitListener() {
        public void onInit(int status) {
            if (status != TextToSpeech.ERROR) {
                 tts.setLanguage(Locale.US);
                 speakout("Hello Gies");
            }
        }
    });
}

public void speakout(String text) {
    tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    tv2.setText("" + text);

}

@Override
protected void onPause() {
    if (tts != null) {
        tts.stop();
        tts.shutdown();
    }
    super.onPause();
}

The code runs fine. I am trying to covert the text to speech but the I am unable to find the desired output. Please help me out with a fix. Thanks in advance.

You should not use tts reference before onInit is called with success, and from your code its clear you are calling speakout just after creating TextToSpeech class. Move speakout("Hello Gies"); to inside your onInit . Also if you shutdown your tts in onPause then you better recreate it in onResume - this means you can actually move your init(); to onResume .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM