Android - Speech Recognition Limit listening time

I use the Google API for speech recognition, but I want to limit the listening time. For example, two seconds. After two seconds, although the user continues to recognize conversations, he should stop listening. I tried several EXTRA for example

EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS

but it didn’t help me. My complete code is here, if anyone can help me, I will 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; } } 
+5
source share
1 answer

You cannot limit the listening time of the recognizer. Just set the minimum time that he needs to listen before closing, but not the maximum.

I was also looking for a solution to this problem. Therefore, I hope that maybe you will find the best. I found this post from another StackOverflow helper:

SpeechRecognizer Time Limit

There he offers the following opportunity to fix your problem:

It would be best to sink some kind of timer, something like 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(); 

Otherwise, to make your SpeechRecognition short, you can add the following parameter to your intent: EXTRA_PARTIAL_RESULTS

This will allow you to get partial results from your SpeechRecognizer, which means that your onActivityPartialResult method will return you another array with match values. This method is called before onActivityResults and is faster, but certainly not as accurate as onActivityResult. So this will help you if your listener is looking for a specific word.

Hope this helps you a bit with your application!

Good luck

+2
source

Source: https://habr.com/ru/post/1247960/


All Articles