如何完成Text to Speech .stop()

时间:2016-07-08 18:31:49

标签: android text-to-speech

帮助我解决小问题。我有TextToSpeech mTTS;变量和setOnUtteranceProgressListener(...)然后SpeakText(...)开始发言部分文字; OnClick按钮我试图停止言语SpeakStop(),但出了点问题?!语音停止然后被解雇onDone()并且语音继续......(有时不是) 如何.stop语音保证?!

TextToSpeech mTTS;
mTTS = new TextToSpeech(this, new TextToSpeech.OnInitListener() { ... }


mTTS.setOnUtteranceProgressListener(new UtteranceProgressListener() {
            @Override
            public void onDone(String utteranceId) {
                bWaitForSpeakDone = false;
                // ...
                SpeakText(utteranceId); // speak next part text
            }

            @Override
            public void onError(String utteranceId) {
                bWaitForSpeakDone = false;
            }

            @Override
            public void onStart(final String utteranceId) {
                bWaitForSpeakDone = true;
                // ...
            }
        });

void SpeakStop() {
        mTTS.stop();
        bWaitForSpeakStop = true;
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                boolean isException = false;
                AddDebugText("Wait for mTTS.stop()");
                while (mTTS.isSpeaking() || bWaitForSpeakDone) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        // e.printStackTrace();
                        AddDebugText("Thread exception (");
                        // isException = true;
                    }
                }
                bWaitForSpeakStop = false;
            }
        });
    }

0 个答案:

没有答案