简体   繁体   English

TextToSpeech.stop()无法正常工作

[英]TextToSpeech.stop() is not working

I am calling text to speech speak() continously in onUteranceComplete() method, when an event occurs. 当事件发生时,我会在onUteranceComplete()方法中连续调用文本到语音的onUteranceComplete() The speak method is working fine and it is continously speaking the text but I am facing a weird problem in this code. 语音方法运行良好,可以连续朗读文本,但是在此代码中我遇到了一个奇怪的问题。 When the event finishes, I call textToSpeech.stop() method, but the speak method keeps speaking and it seems like stop() isn't working at all. 事件结束后,我调用了textToSpeech.stop()方法,但是speak方法保持发言状态,似乎stop()根本无法工作。 I guess the problem is occuring because of calling speak() continously in onUteranceCOmplete(). 我猜是由于在onUteranceCOmplete()中连续调用了talk()而引起了问题。

What do you experts think, what is the cause of this problem? 您的专家认为,此问题的原因是什么?

        @Override
        public void onInit(int status) {

            if(status != TextToSpeech.ERROR)
            {
                Toast.makeText(getApplicationContext(), "text to speech created", Toast.LENGTH_SHORT).show();
                tts.setLanguage(Locale.US);
                tts.setPitch(1.2f);
                tts.setSpeechRate(1.0f);

                tts.setOnUtteranceCompletedListener(new OnUtteranceCompletedListener() {

                    @Override
                    public void onUtteranceCompleted(String arg0) {
                        // TODO Auto-generated method stub

                        tts.speak("hello world", TextToSpeech.QUEUE_FLUSH, hash);

                    }
                });
            }

    @Override
    public void onCallStateChanged(int state, final String incomingNumber) {
        super.onCallStateChanged(state, incomingNumber);
       ....
       x = tts.speak("hello world", TextToSpeech.QUEUE_FLUSH, hash);
   }

Regards 问候

You should set a class member flag 您应该设置一个班级成员标志

private boolean mShouldSpeak = true;  

and then check the flag in onUtteranceCompleted 然后检查onUtteranceCompleted中的标志

tts.setOnUtteranceCompletedListener(new OnUtteranceCompletedListener() {

                @Override
                public void onUtteranceCompleted(String arg0) {
                    // TODO Auto-generated method stub
                    if (mShouldSpeak) 
                    {
                        tts.speak("hello world", TextToSpeech.QUEUE_FLUSH, hash);
                    }

                }
            });

When the event finished just set mShouldSpeak to false 事件结束后,只需将mShouldSpeak to false设置mShouldSpeak to false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM