3

我正在使用android.speech.SpeechRecognizer并且遇到一个问题,即使在我调用了它的 , 和方法之后它也会发出独特的stopListening()铿锵cancel()destroy()

这是我创建和销毁SpeechRecognizerin 的方法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 上测试代码,并使用minSdkVersion21 和targetSdkVersion28 进行编译。

谁能告诉我我可能做错了什么或者图书馆中是否有错误?

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

4

1 回答 1

0

你确定你需要打电话SpeechRecognizer.cancel()吗?根据这个答案调用SpeechRecognizer.destroy()可能就足够了。

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

于 2020-05-07T05:22:50.977 回答