在使用文本到语音之前停止语音识别

时间:2013-05-06 14:25:26

标签: android speech-recognition text-to-speech

我正在使用语音识别和文本到语音来实现对话应用程序。我注意到,一旦识别器启动,它会尝试识别任何声音,包括文本到语音的结果。

我尝试了下面的代码,以防止它听TTS,但我得到了这个例外:

E/JavaBinder(29640): *** Uncaught remote exception!  (Exceptions are not yet supported across processes.)
 E/JavaBinder(29640): java.lang.RuntimeException: SpeechRecognizer should be used only from the application's main thread
 E/JavaBinder(29640):   at android.speech.SpeechRecognizer.checkIsCalledFromMainThread(SpeechRecognizer.java:319)
 E/JavaBinder(29640):   at android.speech.SpeechRecognizer.stopListening(SpeechRecognizer.java:303)
 E/JavaBinder(29640):   at com.example.mycode.SpeechActivity$2.onStart(SpeechActivity.java:188)
 E/JavaBinder(29640):   at android.speech.tts.TextToSpeech$Connection$1.onStart(TextToSpeech.java:1280)
 E/JavaBinder(29640):   at android.speech.tts.ITextToSpeechCallback$Stub.onTransact(ITextToSpeechCallback.java:55)
 E/JavaBinder(29640):   at android.os.Binder.execTransact(Binder.java:367)
 E/JavaBinder(29640):   at dalvik.system.NativeStart.run(Native Method)
 E/SpannableStringBuilder(29640): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
 E/SpannableStringBuilder(29640): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length

代码 -

UtteranceProgressListener ttsProgressListener = new UtteranceProgressListener(){

        @Override
        public void onDone(String arg0) {
            // TODO Auto-generated method stub
            recognizer.startListening(intent);
        }

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

        }

        @Override
        public void onStart(String utteranceId) {
            // TODO Auto-generated method stub
            recognizer.stopListening();
        }

    };

你能帮我调试吗?

1 个答案:

答案 0 :(得分:2)

试试这个 - >

@Override
public void onStart(String utteranceId) {
    // TODO Auto-generated method stub
    SpeechActivity.this.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    recognizer.stopListening();
                }
            });
}