Is there a way to get SpeechRecognizer
to listen, wait for one word, then stop listening? I only need the first word that the user speaks.
You need to use:
EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS
flag rather than:
SPEECH_INPUT_MINIMUM_LENGTH_MILLIS
flag.
If we set EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS
flag than the speech recognition will terminate after the specified time in millis of silence. So in your case the user speaks a word and pauses for 100 milliseconds it will give you the word only. Below is the code I've used and tested also. Edit the time of silence as per your requirement. Just call this function in your activity for testing.
public void provideTextToConvert()
{
Intent speechintent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
speechintent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS , 100);
speechintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
speechintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
speechintent.putExtra(RecognizerIntent.EXTRA_PROMPT,
getString(R.string.speech_prompt));
try {
startActivityForResult(speechintent, REQ_CODE_SPEECH_INPUT);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With