在使用文本到语音之前停止语音识别 [英] Stopping speech recognition before using text to speech

查看:59
本文介绍了在使用文本到语音之前停止语音识别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我尝试了下面的代码来阻止它收听 TTS,但我得到了这个异常:

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

代码 -

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

    };

你能帮我调试一下吗?

推荐答案

试试这个->

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

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

这篇关于在使用文本到语音之前停止语音识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆