【问题标题】:How to prevent Android SpeechRecognizer from making noise after being destroyed?如何防止Android SpeechRecognizer被破坏后发出噪音?
【发布时间】:2020-04-27 23:06:16
【问题描述】:

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

这是我在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

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

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

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

【问题讨论】:

    标签: android kotlin android-speech-api


    【解决方案1】:

    您确定需要拨打SpeechRecognizer.cancel() 吗?根据this 回答调用SpeechRecognizer.destroy() 可能就足够了。

    还要注意您的日志中没有出现connected to the recognition service 错误,这表明 SpeechRecognizer 出现问题。您可以尝试去掉cancel()的调用,检查错误是否消失。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-09
      • 1970-01-01
      • 2016-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多