gpt4 book ai didi

android - 语音搜索/SpeechRecognizer 在快速启动/停止收听时崩溃

转载 作者:行者123 更新时间:2023-11-29 21:42:11 25 4
gpt4 key购买 nike

我有一个使用 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;
}

问题是当我在 Activity 开始时快速点击按钮时,强制关闭通知出现在这个错误日志中:

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)

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

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

最佳答案

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

关于android - 语音搜索/SpeechRecognizer 在快速启动/停止收听时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16783246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com