There are a number of approaches to this.
Using a timer
You could thread some sort of a count down timer that counts down for the user. This post, discusses this idea more: SpeechRecognizer Time Limit.
In that post, the following is proposed:
Your best bet would be to thread some sort of timer, something like a
CountDownTimer:
yourSpeechListener.startListening(yourRecognizerIntent);
new CountDownTimer(2000, 1000) {
public void onTick(long millisUntilFinished) {
//do nothing, just let it tick
}
public void onFinish() {
yourSpeechListener.stopListening();
} }.start();
You can use the EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS RecognizerIntent
Using it to control the amount of silence the recognizer needs to hear before stopping. There are two parameters that can work in a situation like this
EXTRA_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS
or EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS
Both serve almost the same purpose, if there is complete_silence for 100ms or possibly for 200ms then the recognizer will stop listening and subsequently process the word that has just been heard. For example:
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS, 2000000);
or
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS, 2000000);