0

I have a button (with a play icon as background) that starts reading some text (text to speech).

I want to change my button background (using a stop icon) while tts is speaking. Then, when it stop speaking i want to reset the button background to the first one (play icon).

I'm trying to do it using a thread but i'm not getting the wish result..

please take a look at my code on button click listener here:

if (tts.isSpeaking()) {
                        tts.stop();
                        btn4.setBackgroundResource(R.drawable.sound_icon);


                    } else {
                         Thread splashTread =  new Thread() {
                                @Override
                                public void run() {
                                    try {

                                        while (tts.isSpeaking()) {
                                            sleep(100);

                                            btn4.setBackgroundResource(R.drawable.stop_icon);

                                        }
                                    } catch (Exception e) {
                                        // do nothing
                                    } finally {
                                        btn4.setBackgroundResource(R.drawable.sound_icon);

                                    }
                                }
                            };


                        if (myText.equals("")) {
                            speech("No text");
                            splashTread.start();

                        } else {
                        speech(myText);
                        splashTread.start();

                    }
menu_on_top
  • 2,613
  • 14
  • 44
  • 71

3 Answers3

1

So i never used TTS but very fast search bring me that callback : onutterancecompleted

Check also that post : How to know when TTS is finished?

It looks like you have a callback wwhen tts starts, onInit() where you can change background, and another one, onutterancecompleted where you can revert background.

Tell me if it works, I will try on my spare time.

Community
  • 1
  • 1
Poutrathor
  • 1,990
  • 2
  • 20
  • 44
  • I see that your solution may be the correct one..But please let me check it a little more because there are some issues :) – menu_on_top Nov 06 '13 at 17:27
1

This is a rather bad way of checking when the speech is finished. Instead you should use setOnUtteranceProgressListener to check when the speech has finished. Your code could be very simple:

if (tts.isSpeaking()) {
    tts.stop();
    btn4.setBackgroundResource(R.drawable.sound_icon);
} else {
    tts.setOnUtteranceProgressListener(new OnUtteranceProgressListener() {
        public onDone(String utteranceId) {
            btn4.setBackgroundResource(R.drawable.sound_icon);
        }
        void onError(String utteranceId) {
            btn4.setBackgroundResource(R.drawable.stop_icon);
        }
        void onStart(String utteranceId) { }
    });

    if (myText.equals("")) {
        speech("No text");
    }
    else {
        speech(myText);
    }
}
Aleks G
  • 56,435
  • 29
  • 168
  • 265
  • Hey,thanks for your answer!Unfortunately,i cant use setOnUtteranceProgressListener because all this job is placed inside a button onClick listener.. – menu_on_top Nov 06 '13 at 17:10
  • @menu_on_top I don't understand why it matters. You simply put the above code inside your `onClick` event. – Aleks G Nov 07 '13 at 08:58
1

For instance you might want to change the background after myTextView has finished synthesizing like this

ashMap<String, String> myHashAlarm = new HashMap();
myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_STREAM,
        String.valueOf(AudioManager.STREAM_ALARM));
mTts.speak(myText1, TextToSpeech.QUEUE_FLUSH, myHashAlarm);
mTts.speak(myText2, TextToSpeech.QUEUE_ADD, myHashAlarm);

We will use an optional parameter, to tag our utterance as one we want to identify. We also need to make sure our activity implements the

TextToSpeech.OnUtteranceCompletedListener interface:

mTts.setOnUtteranceCompletedListener(this);
myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_STREAM,
        String.valueOf(AudioManager.STREAM_ALARM));
mTts.speak(myText1, TextToSpeech.QUEUE_FLUSH, myHashAlarm);
myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,
        "end of message");
// myHashAlarm now contains two optional parameters
mTts.speak(myText2, TextToSpeech.QUEUE_ADD, myHashAlarm);

And the Activity gets notified of the completion in the implementation of the listener:

public void onUtteranceCompleted(String uttId) {
    if (uttId == "end of message") {
        // Do your work here
        btn4.setBackgroundResource(R.drawable.stop_icon);
    } 
}

HashMap is used to wake up the user after tts complete.

Ankit Dhadse
  • 1,566
  • 1
  • 15
  • 19