Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Speech recognition one word?

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.

like image 379
g2214789 Avatar asked Sep 12 '25 06:09

g2214789


1 Answers

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();
    }
}
like image 88
Saurav Avatar answered Sep 13 '25 19:09

Saurav