【问题标题】:Stopping speech recognition before using text to speech在使用文本转语音之前停止语音识别
【发布时间】:2013-05-06 14:25:25
【问题描述】:

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

我尝试了下面的代码来阻止它收听 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();
        }

    };

你能帮我调试一下吗?

【问题讨论】:

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


    【解决方案1】:

    试试这个->

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

    【讨论】:

    • 非常感谢 :) 这解决了异常,我从没想过。祝你有美好的一天,上帝保佑!
    • 你对这个问题有什么想法stackoverflow.com/questions/15759676/… 吗?我正在尝试连续收听,但它会随机停止收听,但在 logcat 上它会显示 ReadyForSpeech 但在我手动重新启动它之前它不会选择任何内容
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多