I'm trying to take some action after a TextToSpeech object in my Android app finishes speaking a sentence, but my UtteranceProgressListener.onDone()
method is never called. I tried many things, including the suggestions from this post, but nothing has worked. Relevant code from my app is posted below. The TextToSpeech object correctly speaks the sentence I give it, but none of the callback functions in UtteranceProgressListener
are called. Can someone please help me identify what's wrong? For example, should the utterance ID I provide to the TextToSpeech.speak()
function need to be in some special format that I'm missing?
mtts = new TextToSpeech(myAct, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
mtts.setLanguage(Locale.US);
}
}
});
mtts.setOnUtteranceProgressListener(new UtteranceProgressListener() {
@Override
public void onDone(String utteranceId) {
Toast.makeText(myAct, "OnDone called", Toast.LENGTH_LONG).show();
}
@Override
public void onError(String utteranceId) {
Toast.makeText(myAct, "OnError called", Toast.LENGTH_LONG).show();
}
@Override
public void onStart(String utteranceId) {
Toast.makeText(myAct, "OnStart called", Toast.LENGTH_LONG).show();
}
});
Bundle params = new Bundle();
params.putString(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "");
myAct.mtts.speak(myText, TextToSpeech.QUEUE_FLUSH, params, "MyID");
If you want to add the call back UtteranceProgressListener, you have to give TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID as the utteranceId
It would look like this:
See this post: UtteranceProgressListener does not return error message
and check the engine web site: TTS Engine