onUtteranceCompleted does not get called?

2019-01-26 06:51发布

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.

1条回答
Ridiculous、
2楼-- · 2019-01-26 07:32

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);
查看更多
登录 后发表回答