androidspeech-recognitionrecognizer-intent

Android - Speech Recognition Limiting Listening Time


I am using Google API for speech recognition, but want to limit listening time. For example two seconds. After two seconds even though user continue to speaking recognizer should stop listening. I tried some EXTRAs like

EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS

but it did not helped me. My full code is here, if anyone can help me, I will be appreciate

public void promptSpeechInput()
{
    //This intent recognize the peech
    Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    i.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
    i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Say Something");

    try {
        startActivityForResult(i, 100);
    }
    catch (ActivityNotFoundException a)
    {
        Toast.makeText(MainActivity.this,"Your device does not support",Toast.LENGTH_LONG).show();
    }
}

//For receiving speech input
public void onActivityResult(int request_code, int result_code, Intent i)
{
    super.onActivityResult(request_code, result_code, i);

    switch (request_code)
    {
        case 100: if(result_code == RESULT_OK && i != null)
        {
            ArrayList<String> result = i.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            resultTEXT.setText(result.get(0));
        }
            break;
    }
}

Solution

  • You can't limit the time that the recognizer is listening. Just set the minimum time that he need to listen before close, but not a max one.

    I have been looking to a solution for this problem too, So I hope that maybe you will find a better one. I found this post from another StackOverflow mate:

    SpeechRecognizer Time Limit

    There, he propose the next possibility to fix your problem:

    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();
    

    In other way, to make your SpeechRecognition short, you can add the next parameter to your Intent: EXTRA_PARTIAL_RESULTS

    This will let you get partial results from your SpeechRecognizer, which means that your methoronActivityPartialResult will return you another array with matches value. This method is called before onActivityResults and its more fast, but of course is not as precise as onActivityResult. So that will just help you if your Listener is looking for a specific word.