1

我有一个使用 SpeechRecognizer 的应用程序(在 onCreate 函数中):

mIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
mIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, this.getPackageName());
mRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
mRecognizer.setRecognitionListener(this);

我有一个按钮可以启动和停止语音识别(在 onClick 中):

if (view.getId() == R.id.btnSpeak) {
    if (mIsRecording) {
        mRecognizer.stopListening();
        mBtnSpeak.setBackgroundResource(R.drawable.mic);
        mIsCanceled = true;

    } else {
        mRecognizer.startListening(mIntent);
        mBtnSpeak.setBackgroundResource(R.drawable.mic_green);
    }

    mIsRecording = !mIsRecording;
}

问题是当我在活动开始时快速点击按钮时,会出现强制关闭通知,并带有以下错误日志:

05-28 11:35:19.460: E/AndroidRuntime(9288): FATAL EXCEPTION: main
05-28 11:35:19.460: E/AndroidRuntime(9288): java.lang.NullPointerException
05-28 11:35:19.460: E/AndroidRuntime(9288):     at com.google.android.voicesearch.speechservice.MicrophoneManagerImpl.stopListening(MicrophoneManagerImpl.java:195)
05-28 11:35:19.460: E/AndroidRuntime(9288):     at com.google.android.voicesearch.speechservice.RecognitionControllerImpl.onStopListening(RecognitionControllerImpl.java:280)
05-28 11:35:19.460: E/AndroidRuntime(9288):     at com.google.android.voicesearch.GoogleRecognitionService.onStopListening(GoogleRecognitionService.java:58)
05-28 11:35:19.460: E/AndroidRuntime(9288):     at android.speech.RecognitionService.dispatchStopListening(RecognitionService.java:118)
05-28 11:35:19.460: E/AndroidRuntime(9288):     at android.speech.RecognitionService.access$100(RecognitionService.java:36)
05-28 11:35:19.460: E/AndroidRuntime(9288):     at android.speech.RecognitionService$1.handleMessage(RecognitionService.java:82)
05-28 11:35:19.460: E/AndroidRuntime(9288):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-28 11:35:19.460: E/AndroidRuntime(9288):     at android.os.Looper.loop(Looper.java:130)
05-28 11:35:19.460: E/AndroidRuntime(9288):     at android.app.ActivityThread.main(ActivityThread.java:3746)
05-28 11:35:19.460: E/AndroidRuntime(9288):     at java.lang.reflect.Method.invokeNative(Native Method)
05-28 11:35:19.460: E/AndroidRuntime(9288):     at java.lang.reflect.Method.invoke(Method.java:507)
05-28 11:35:19.460: E/AndroidRuntime(9288):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:895)
05-28 11:35:19.460: E/AndroidRuntime(9288):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:653)
05-28 11:35:19.460: E/AndroidRuntime(9288):     at dalvik.system.NativeStart.main(Native Method)
05-28 11:35:19.460: E/AndroidRuntime(9288): [Blue Error Handler] Make Debugging Report file for main
05-28 11:35:19.460: E/AndroidRuntime(9288): java.lang.NullPointerException
05-28 11:35:19.460: E/AndroidRuntime(9288):     at com.google.android.voicesearch.speechservice.MicrophoneManagerImpl.stopListening(MicrophoneManagerImpl.java:195)
05-28 11:35:19.460: E/AndroidRuntime(9288):     at com.google.android.voicesearch.speechservice.RecognitionControllerImpl.onStopListening(RecognitionControllerImpl.java:280)
05-28 11:35:19.460: E/AndroidRuntime(9288):     at com.google.android.voicesearch.GoogleRecognitionService.onStopListening(GoogleRecognitionService.java:58)
05-28 11:35:19.460: E/AndroidRuntime(9288):     at android.speech.RecognitionService.dispatchStopListening(RecognitionService.java:118)
05-28 11:35:19.460: E/AndroidRuntime(9288):     at android.speech.RecognitionService.access$100(RecognitionService.java:36)
05-28 11:35:19.460: E/AndroidRuntime(9288):     at android.speech.RecognitionService$1.handleMessage(RecognitionService.java:82)
05-28 11:35:19.460: E/AndroidRuntime(9288):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-28 11:35:19.460: E/AndroidRuntime(9288):     at android.os.Looper.loop(Looper.java:130)
05-28 11:35:19.460: E/AndroidRuntime(9288):     at android.app.ActivityThread.main(ActivityThread.java:3746)
05-28 11:35:19.460: E/AndroidRuntime(9288):     at java.lang.reflect.Method.invokeNative(Native Method)
05-28 11:35:19.460: E/AndroidRuntime(9288):     at java.lang.reflect.Method.invoke(Method.java:507)
05-28 11:35:19.460: E/AndroidRuntime(9288):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:895)
05-28 11:35:19.460: E/AndroidRuntime(9288):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:653)
05-28 11:35:19.460: E/AndroidRuntime(9288):     at dalvik.system.NativeStart.main(Native Method)

它不会使应用程序崩溃,但我想防止这种情况发生。根据我从日志中了解到的情况,我的代码在语音识别器准备好之前称为停止侦听。有没有办法检查是否可以停止监听/开始监听?

此外,只有当我在活动开始时快速点击它时才会出现该错误。否则,speechrecognizer 只会调用 onError,我可以从那里更新我的按钮。我正在考虑添加一个延迟,让语音识别器有时间处理事情,但感觉很笨拙。

4

2 回答 2

1

而不是mRecognizer.stopListening();你应该调用mRecognizer.cancel();
如果操作系统是 JB,你必须实现倒数计时器,请参阅Android Speech Recognition as a service on Android 4.1 & 4.2

于 2013-05-28T03:16:32.617 回答
1

您不能完全用取消替换停止并说它解决了快速停止问题,因为取消不一样 -

cancel将停止识别器并且不显示任何结果。
stopListening将停止录音并触发onResults监听器回调,目前为止的录音转录。

stopListening在我的手机上(OS 4.4.2) - 我发现在发生之前打电话onReadyForSpeech会导致识别器卡住!No onResultsoronError被调用并尝试再次启动它会导致 BUSY 错误。

解决方案 - 仅将 mIsRecording 设置为 trueonReadyForSpeech

当您只想stopListening在标志为真时停止识别器调用时。如果它为假,则cancel改为调用并手动触发结束事件。

于 2014-10-22T09:55:17.857 回答