我正在使用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 进行编译,测试代码。
有没有人能告诉我可能出了什么问题,或者库中是否存在错误?
目前,我有一个笨拙的解决方法,在关闭识别器后将媒体音频流静音。