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.
One solution to handle end or error cases is to set a
RecognitionListenerto yourSpeechRecognizerinstance. You must doing it before callingstartListening()!Example:
In your case, you can make your class containing the
mIsRecordingattribute implementRecognitionListenerinterface. Then, you just have to override these two methods with the following instruction:Furthermore, your
mIsRecording = trueinstruction is in the wrong place. you should do it in theonReadyForSpeech(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:
Be careful about thread safety for recordingIsRunning calls, and all will be ok :)