我正在使用 android.speech.SpeechRecognizer ,即使我调用了stopListening()cancel()destroy()方法后,它仍发出独特的c声。

这是我在 SpeechRecognizer 中创建和销毁MainActivity.kt的方法。

private fun startSpeechRecognition() {
    Log.e(TAG, "At start of startSpeechRecognition()")
    if (recognizer == null) {
        recognizer = SpeechRecognizer.createSpeechRecognizer(this)
        Log.e(TAG, "Creating new recognizer: $recognizer")
        recognizer?.setRecognitionListener(Listener())
    }
    val intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
    intent.putExtra(
        RecognizerIntent.EXTRA_LANGUAGE_MODEL,
        RecognizerIntent.LANGUAGE_MODEL_FREE_FORM
    )
    Log.e(TAG, "Starting listening")
    recognizer?.startListening(intent)
}

private fun closeRecognizer() {
    Log.e(TAG, "At start of closeRecognizer()")
    recognizer?.run {
        Log.e(TAG, "Stopping recognizer: $this")
        stopListening()
        cancel()
        destroy()
        recognizer = null
    } ?: Log.e(TAG, "Recognizer already null")
}

这是我的日志:
E/voice.assistan: Unknown bits set in runtime_flags: 0x8000
E/MainActivity: At start of closeRecognizer()
E/MainActivity: Recognizer already null
E/MainActivity: At start of startSpeechRecognition()
E/MainActivity: Creating new recognizer: android.speech.SpeechRecognizer@573d161
E/MainActivity: Starting listening
E/MainActivity: At start of closeRecognizer()
E/MainActivity: Stopping recognizer: android.speech.SpeechRecognizer@573d161
E/SpeechRecognizer: not connected to the recognition service
E/SpeechRecognizer: not connected to the recognition service
E/MainActivity: At start of closeRecognizer()
E/MainActivity: Recognizer already null

我正在运行Android 10的Pixel 2上测试代码,并使用minSdkVersion 21和targetSdkVersion 28进行编译。

谁能告诉我我可能做错了什么,或者库中是否有错误?

我目前有一个笨拙的解决方法,在关闭识别器后我将媒体音频流静音。

最佳答案

确定要调用SpeechRecognizer.cancel()吗?根据this的回答,调用SpeechRecognizer.destroy()就足够了。

另外请注意,您的日志中没有connected to the recognition service错误,这表明SpeechRecognizer中出现问题。您可以尝试删除对cancel()的调用并检查错误是否消失。

关于android - 如何防止Android SpeechRecognizer在销毁后发出噪音?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61469946/

10-15 20:27