Stopping speech recognition before using text to s

2019-08-20 03:51发布

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条回答
地球回转人心会变
2楼-- · 2019-08-20 04:29

Try this- >

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

                @Override
                public void run() {
                    recognizer.stopListening();
                }
            });
}
查看更多
登录 后发表回答