我正在使用android.speech.SpeechRecognizer
并且遇到一个问题,即使在我调用了它的 , 和方法之后它也会发出独特的stopListening()
铿锵cancel()
声destroy()
。
这是我创建和销毁SpeechRecognizer
in 的方法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 进行编译。
谁能告诉我我可能做错了什么或者图书馆中是否有错误?
我目前有一个笨拙的解决方法,我在关闭识别器后将媒体音频流静音。