Shubhankar
Shubhankar

Reputation: 95

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.

Upvotes: 0

Views: 1202

Answers (1)

marcinj
marcinj

Reputation: 49986

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.

Upvotes: 2

Related Questions