androidtext-to-speechonutterancecompleted

onUtteranceCompleted does not get called?


Even though I am setting it correctly:

HashMap<String, String> myHashRender = new HashMap<String, String>();
myHashRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "utid");
mTts.speak("Speak something", TextToSpeech.QUEUE_ADD, myHashRender);

also

mTts.setOnUtteranceCompletedListener(this);

in the onInit function return success. Still the onUtteranceCompleted does not get called. Though there are duplicate questions, but no where I could find the answer.

My Activity also implements OnUtteranceCompletedListener.

Please help.


Solution

  • Call the setOnUtteranceCompletedListener inside the onInit function of the tts object.

    If you want to make any changes to the UI on the call of the onUtteranceCompleted function, add the code inside a runOnUIThread method.

    And do remember to add the Hashmap param value while calling the speak() function

    Example :

    TextToSpeech tts= new TextToSpeech(context, new OnInitListener() {
    
     @Override
     public void onInit(int status) {
    
        mTts.setOnUtteranceCompletedListener(new OnUtteranceCompletedListener() {
    
            @Override
            public void onUtteranceCompleted(String utteranceId) {
    
                runOnUiThread(new Runnable() {
    
                    @Override
                    public void run() {
                    //UI changes
                    }
                });
            }
        });
    
     }
    });
    
    
    HashMap<String, String> params = new HashMap<String, String>();
    
    params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,"stringId");
    
    tts.speak("Text to Speak",TextToSpeech.QUEUE_FLUSH, params);