How to Know When Tts Is Finished

How to know when TTS is finished?

public class TTSActivity extends Activity implements OnInitListener, OnUtteranceCompletedListener, ... {
private TextToSpeech mTts;
...........
private void speak(String text) {
if(text != null) {
HashMap<String, String> myHashAlarm = new HashMap<String, String>();
myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_ALARM));
myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "SOME MESSAGE");
mTts.speak(text, TextToSpeech.QUEUE_FLUSH, myHashAlarm);
}
}
// Fired after TTS initialization
public void onInit(int status) {
if(status == TextToSpeech.SUCCESS) {
mTts.setOnUtteranceCompletedListener(this);
}
}
// It's callback
public void onUtteranceCompleted(String utteranceId) {
Log.i(TAG, utteranceId); //utteranceId == "SOME MESSAGE"
}
...........
}

Read A good tutorial

How to know when TextToSpeech is finished with speaking?

The problem is not in onUtteranceComplete function , its most likely get called, but you're trying to print the output to System.out , please use android.utils.Log("tag", "success") instead and see what happens.

Also You need to set UtteranceID to get onUtteranceComplete to work
before calling tts.speak method. so to sum up:

            TextToSpeech tts= new TextToSpeech(MainActivity.this, new OnInitListener() {

@Override
public void onInit(int status) {

tts.setOnUtteranceCompletedListener(new OnUtteranceCompletedListener() {

@Override
public void onUtteranceCompleted(final String utteranceId) {
System.out.println("Completed");

runOnUiThread(new Runnable() {

@Override
public void run() {
//UI changes
}
});
}
});

}
});
Map<String,String> ttsParams = new HashMap<String, String>();
ttsParams.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,
MainActivity.this.getPackageName());

mTts.speak(text, TextToSpeech.QUEUE_FLUSH, ttsParams);

And the exception you're getting has nothing to do with the onUtteranceComplete method, if it is because of tts engine most likely you're not requesting to destroy your instance when finishing your activity.

To fix that add

@Override
onDestroy(){
super.onDestroy();
mTts.shutdown(); //mTts is your TextToSpeech Object
}

To your activity class

How can I Toast after Text to Speech finish speaking Android

You try to show a Toast in a thread that is not the UI(main) thread.
You should change this

@Override
public void onUtteranceCompleted(String utteranceId) {
Log.i("CALLBACK", utteranceId); //utteranceId == "SOME MESSAGE"
Toast.makeText(getApplicationContext(),"Call Back",Toast.LENGTH_LONG).show();// I Cannot Toast here. Or do something more than Log
}

into this

@Override
public void onUtteranceCompleted(String utteranceId) {
Log.i("CALLBACK", utteranceId); //utteranceId == "SOME MESSAGE"

runOnUiThread(new Runnable() {

public void run() {
Toast.makeText(getApplicationContext(),"Call Back",Toast.LENGTH_LONG).show();
}
});
}

That way your code is dispatched to the main thread where you are allowed to show Toasts



Related Topics



Leave a reply



Submit