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();

                    }
4

3 回答 3

1

所以我从未使用过 TTS,但非常快速的搜索给我带来了回调:onutterancecompleted

另请查看该帖子:如何知道 TTS 何时完成?

看起来您在 tts 启动时有一个回调,您可以在其中更改背景的 onInit() 以及可以在其中恢复背景的另一个 onutterancecompleted。

告诉我它是否有效,我会在空闲时间尝试。

于 2013-11-06T17:01:53.497 回答
1

例如,您可能希望在 myTextView 完成像这样的合成后更改背景

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);

我们将使用一个可选参数,将我们的话语标记为我们想要识别的话语。我们还需要确保我们的活动实现

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);

并且 Activity 在侦听器的实现中收到完成通知:

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

HashMap用于wake uptts 完成后的用户。

于 2013-11-06T18:22:08.550 回答
1

这是检查演讲何时结束的一种相当糟糕的方法。相反,您应该使用setOnUtteranceProgressListener来检查演讲何时结束。您的代码可能非常简单:

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);
    }
}
于 2013-11-06T17:07:25.773 回答