0

In Android TextToSpeech, How come we know either Speech is finished. I want to change icon after speech finished. I declared tts.speak() in Button Onclick Listener. i used thread postdelayed handler to change icon after some time. but the text to read changes different at different times.It didnt worked out. CheckThemestart(),ChangeThemeStop() are functions to change the icon.

i declared the following in OnCreate() :

            tts = new TextToSpeech(getContext(), new 
                         TextToSpeech.OnInitListener() {
           @Override
           public void onInit(int status) {
            if (status != TextToSpeech.ERROR) {
                tts.setLanguage(Locale.UK);
            tts.speak("",TextToSpeech.QUEUE_FLUSH,null);
            }

Button-onClickListener :

 if (!tts.isSpeaking()) {
                    CheckThemeStart();
                    tts.speak(plainText, TextToSpeech.QUEUE_FLUSH, null);

                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                             //change icon after spoken
                            CheckThemeStop();

                        }
                    }, 15000);

                } else {
                    CheckThemeStop();
                    tts.stop();
                }
Ragavendra M
  • 442
  • 5
  • 15
  • did you try setOnUtteranceProgressListener ? – Reyansh Mishra Oct 18 '17 at 05:57
  • Possible duplicate of [How to know when TTS is finished?](https://stackoverflow.com/questions/4658376/how-to-know-when-tts-is-finished) – Reyansh Mishra Oct 18 '17 at 05:59
  • i tried with setOnUtterenceProgressListener. but it didnt worked out, i declared the below next to tts.speak(): tts.setOnUtteranceCompletedListener(new TextToSpeech.OnUtteranceCompletedListener() { @Override public void onUtteranceCompleted(String utteranceId) { CheckThemeStop(); } }) – Ragavendra M Oct 18 '17 at 07:36

2 Answers2

1

You can register UtteranceProgressListener for tts start and end times and error handling.

tts.setOnUtteranceProgressListener(new UtteranceProgressListener() {
        @Override
        public void onStart(String s) {

        }

        @Override
        public void onDone(String s) {

        }

        @Override
        public void onError(String s) {

        }
      });
kimkevin
  • 2,202
  • 1
  • 26
  • 48
1

If we declare a hasmap parameter with unique string ID and pass that id to compare utterence id if matches it execute what you declare inside onUtterenceCompletedListener(). The following is code snippet.

                    HashMap<String,String> params=new HashMap<String, String>();
                    params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,"SpeakID");
                    tts.speak(plainText, TextToSpeech.QUEUE_FLUSH,params);
                    tts.setOnUtteranceCompletedListener(new TextToSpeech.OnUtteranceCompletedListener() {
                        @Override
                        public void onUtteranceCompleted(String utteranceId) {
                            if(utteranceId.equals("SpeakID"))
                            {
                                    CheckThemeStop();
                            }
                        }
                    });
Ragavendra M
  • 442
  • 5
  • 15