'UtteranceProgressListener not called

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");


Solution 1:[1]

Look at logcat, your code has probably fired: android.view.ViewRootImpl$CalledFromWrongThreadException

If you want to do some GUI-thread stuff, use handler and runnable like this:

public void onDone(String utteranceId) {
  Log.v(TAG, "Speaking done"); // this is OK
  // This is GUI-thread part
  final View v = findViewById(R.id.txtStatus);
  v.getHandler().post(new Runnable() {
    @Override
    public void run() {
      txtStatus.setText("Speaking done");
    }
  });
}

And to start TTS speaking:

tts.speak("bla bla bla", TextToSpeech.QUEUE_FLUSH, null, "1");

Solution 2:[2]

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:

myAct.mtts.speak(myText, TextToSpeech.QUEUE_FLUSH, null, TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID);

See this post: UtteranceProgressListener does not return error message

and check the engine web site: TTS Engine

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jakub Cizek
Solution 2 ladytoky0