如何防止Android SpeechRecognizer在被销毁后造成噪音?

3

我正在使用android.speech.SpeechRecognizer,但是遇到一些问题:即使在调用了stopListening()cancel()destroy()方法之后,它仍会发出一个独特的clang声音。

以下是我在MainActivity.kt中创建和销毁SpeechRecognizer的代码。

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

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

有没有人能告诉我可能出了什么问题,或者库中是否存在错误?

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

1个回答

0

你确定需要调用SpeechRecognizer.cancel()吗?根据this的回答,调用SpeechRecognizer.destroy()可能已经足够。

同时注意你日志中的connected to the recognition service错误,这表明在SpeechRecognizer中出现了问题。你可以尝试移除对cancel()的调用并检查错误是否消失。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接