TTS-UtteranceProgressListener not being called

无人久伴 提交于 2019-12-01 02:39:35

found the answer...

Turns out that the TTS resources I found online were using a single TTS string source, so the third parameter in tts.speak(String text, int queueMode, HashMap params) was set to null.

to anybody having this issue in the future:

if you set the third param to null, there's no ID for the UtteranceProgressListener to track. The fix was creating and initializing a hashmap, then adding to the included array for each new TTS with a new ID could be tracked. Here's the code:

HashMap<String, String> map = new HashMap<String, String>();

then, before calling tts.speak...

map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "UniqueID");

then you can call speak with all params...

tts.speak(text, TextToSpeech.QUEUE_FLUSH, map);

To append to WizardKnight's good answer:

The new API prefers a Bundle so replace the HashMap with a Bundle

Bundle params = new Bundle();
params.putString(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "");

then when you make the speak call

tts.speak(text, TextToSpeech.QUEUE_FLUSH, params, "UniqueID");

The key is to use the ID in the speak call. You can put it in the Bundle, but it will do nothing more for you. It has to be in the speak call to trigger the listener.

I have a wired solution for detecting weather tts has finished speaking.

Step 1: Create this class in your code.

class Waiter extends AsyncTask<Void,Void,Void>{
        @Override
        protected Void doInBackground(Void... voids) {
            while (tts.isSpeaking()){
                try{Thread.sleep(1000);}catch (Exception e){}
            }
            return null;
        }
        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            //TTS has finished speaking. WRITE YOUR CODE HERE
        }
    }

Step 2: Call it when calling tts.speak(...)

tts.speak(...);
new Waiter().execute();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!