Stopping speech recognition before using text to speech

孤人 提交于 2019-12-12 03:03:30

问题


I am implementing a dialogue application using speech recognition and text to speech. I noticed that once the recognizer is started it tries to recognition any sound including the result of the text to speech.

I tried the code below to prevent it to listen to the TTS but I get this exception:

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

Code -

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

    };

Can you help me to debug this?


回答1:


Try this- >

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

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


来源:https://stackoverflow.com/questions/16400840/stopping-speech-recognition-before-using-text-to-speech

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!