4

I am trying to track state of SpeechRecognizer, like that:

private SpeechRecognizer mInternalSpeechRecognizer;
private boolean mIsRecording;

public void startRecording(Intent intent) {
 mIsRecording = true;
 // ...
 mInternalSpeechRecognizer.startListening(intent);
}

Problem with that approach is keeping mIsRecording flag up to date is tough, e.g. if there's ERROR_NO_MATCH error should it be set to false or not?
I am under impression some devices stop recording then and others no.

I don't see any method like SpeechRecognizer.isRecording(context), so I am wondering if there's way to query through running services.

Piotr
  • 1,743
  • 1
  • 26
  • 40

1 Answers1

-2

One solution to handle end or error cases is to set a RecognitionListener to your SpeechRecognizer instance. You must doing it before calling startListening()!

Example:

mInternalSpeechRecognizer.setRecognitionListener(new RecognitionListener() {

    // Other methods implementation

    @Override
    public void onEndOfSpeech() {
        // Handle end of speech recognition
    }

    @Override
    public void onError(int error) {
        // Handle end of speech recognition and error
    }

    // Other methods implementation 
});

In your case, you can make your class containing the mIsRecording attribute implement RecognitionListener interface. Then, you just have to override these two methods with the following instruction:

mIsRecording = false;

Furthermore, your mIsRecording = true instruction is in the wrong place. you should do it in the onReadyForSpeech(Bundle params) method definition, otherwise, speech recognition may never start while this value is true.

Finally, in the class managing it, juste create methods like:

// Other RecognitionListener's methods implementation

@Override
public void onEndOfSpeech() {
    mIsRecording = false;
}

@Override
public void onError(int error) {
    mIsRecording = false;
    // Print error
}

@Override
void onReadyForSpeech (Bundle params) {
    mIsRecording = true;
}

public void startRecording(Intent intent) {
    // ...
    mInternalSpeechRecognizer.setRecognitionListener(this);
    mInternalSpeechRecognizer.startListening(intent);
}

public boolean recordingIsRunning() {
    return mIsRecording;
}

Be careful about thread safety for recordingIsRunning calls, and all will be ok :)

N0un
  • 868
  • 8
  • 31
  • as I mentioned in a question, if `onError` is getting executed it does *not* mean device stopped recording audio. If it's ERROR_NO_MATCH sometimes it stops recording, sometimes not. – Piotr Aug 17 '17 at 18:31
  • And what if you call `stopListening()` in case of `ERROR_NO_MATCH`? – N0un Aug 18 '17 at 10:37
  • But I don't want to stop recording if device wants to continue. – Piotr Aug 18 '17 at 11:34
  • I understand but if an error is notified, will the recording give a result? If not, you should abort it as soon as the error is notified to prepare for a new recording instance. – N0un Aug 21 '17 at 07:19
  • I dont know, as it requires checking on large number of devices. Your answer is almost the same what I am doing currently. I was looking for a different approach, e.g. querying through running services to find out if one of them is launched by SpeechRecognizer. – Piotr Aug 21 '17 at 08:21