12

For various reasons, I need to use the raw SpeechRecognizer API instead of the easier RecognizerIntent (RECOGNIZE_SPEECH) activity.

That means, among other things, that I need to handle RecognitionListener.onError() myself.

In response to some of the errors, I simply want to re-start listening. This looks straightforward but when I just call SpeechRecognizer.startListening() upon error, this sometimes seems to trigger two different errors:

 ERROR/ServerConnectorImpl(619): Previous session not destroyed

and

"concurrent startListening received - ignoring this call"

Which hints that I should have done some cleanup before attempting to call SpeechRecognizer.startListening() again.

If this is true, it means that upon a RecognitionListener error, listening is not automatically stopped and/or canceled.

It is also possible that some errors do stop/cancel listening, while others don't. There are really only 9 SpeechRecognizer errors:

  1. ERROR_NETWORK_TIMEOUT
  2. ERROR_NETWORK
  3. ERROR_AUDIO
  4. ERROR_SERVER
  5. ERROR_CLIENT
  6. ERROR_SPEECH_TIMEOUT
  7. ERROR_NO_MATCH
  8. ERROR_RECOGNIZER_BUSY
  9. ERROR_INSUFFICIENT_PERMISSIONS

Since the documentation isn't very detailed about which error cancels listening and which doesn't, do you happen to know, based on your experience, which errors require doing cleanup (and to which extent) before attempting SpeechRecognizer.startListening() again?

srf
  • 2,410
  • 4
  • 28
  • 41
  • 1
    Not many people would know enough to reply here. I'd suggest you try catching the errors and Log.w them accordingly instead of waiting for an answer. You should be able to find out which errors close it. – SuhailSherif Jun 10 '11 at 15:33

3 Answers3

2

No, cancel is not called when onError is invoked. You can look at the source here.

Femi
  • 64,273
  • 8
  • 118
  • 148
  • the source link is not available. – Fisher Jul 26 '20 at 18:02
  • Indeed: who would have thought the original link would move after almost a decade. Updated. – Femi Jul 27 '20 at 17:26
  • @Femi True. `cancel()` is **not** called when `onError()` is invoked. But one could argue that this is not needed because listening has already been effectively canceled by virtue of the error condition. At least [according the source code](https://stackoverflow.com/q/75074233/2597758). Do you agree? – WebViewer Jan 11 '23 at 05:35
1

you can destroy current session by destroy(). And you can restart it again

kis
  • 11
  • 1
1

Actually Femi, some of the error conditions DO stop the transcription service from listening (SpeechRecognizer.ERROR_SPEECH_TIMEOUT for example). It is not necessary to call destroy, just startlistening again.

Shroud
  • 400
  • 4
  • 7
  • Actually @Shroud, if you look at the [source code](http://hi-android.info/src/android/speech/SpeechRecognizer.java.html) I posted in my original question, you'll see that `SpeechRecognizer.ERROR_SPEECH_TIMEOUT` isn't handled any differently than other errors. All errors are relayed by `mInternalHandler.handleMessage()` to `mInternalListener.onError()`. Which in turn relays it to `Message.obtain(mInternalHandler, MSG_ERROR, error).sendToTarget();`. Where do you see it sopping the transcription service? – srf Jul 01 '12 at 16:09