3

I am using android.speech.SpeechRecognizer and am having a problem where it makes a distinctive clang sound even after I've called its stopListening(), cancel(), and destroy() methods.

Here is how I create and destroy the 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")
}

Here is my log:

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

I am testing the code on a Pixel 2 running Android 10 and am compiling with minSdkVersion 21 and targetSdkVersion 28.

Can anyone tell me what I could be doing wrong or if there is a bug in the library?

I currently have a clumsy workaround where I mute the media audio stream after closing the recognizer.

Ellen Spertus
  • 5,875
  • 9
  • 47
  • 82

1 Answers1

0

Are you sure that you need to call SpeechRecognizer.cancel()? According to this answer calling SpeechRecognizer.destroy() may suffice.

Also notice not connected to the recognition service error in your log, indicating that something goes wrong in SpeechRecognizer. You may try to remove calling of cancel() and check whether the error is gone.

fdermishin
  • 3,167
  • 3
  • 19
  • 42