Speech Recognition Time Too Early in Android

This feature starts speech recognition, but it ends too quickly, where, as if speech recognition is started from the IME keyboard (for example, the Google keyboard), it is not so fast. I need a way to start the same intentions as a Google keyboard.

public void StartSpeechRecognitionActivity(){ try{ Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass().getPackage().getName()); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); intent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true); intent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS, 3000); intent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS, 3000); main.startActivityForResult(intent, SPEECHRECOGNITION_RESULTCODE); } catch (ActivityNotFoundException error) { ShowAndSpeakMessage("Speech recognition is not supported by your device"); } catch( RuntimeException error ) { Log.e( TAG, ERROR_PREFIX + Errors.toString(error) ); } catch( Error error ) { Log.e( TAG, ERROR_PREFIX + Errors.toString(error) ); throw error; } } 
+4
source share
2 answers
 Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Voice recognition!"); startActivityForResult(intent, REQUEST_CODE); 

It just works great for me!

+1
source

Then I think you need to check in the onError function. I do it as follows. and it works great for me.

  public void onError(int error) { String errorMessage = getErrorText(error); Log.d(LOG_TAG+">"+ "FAILED " + errorMessage); if(errorMessage.contains("RecognitionService busy")) { speech.stopListening(); speech.startListening(recognizerIntent); }else if(errorMessage.contains("No speech input")){ speech.stopListening(); speech.startListening(recognizerIntent); }else if(errorMessage.contains("No match")){ speech.stopListening(); speech.startListening(recognizerIntent); } } 

therefore, you can do as many checks as there are types of errors, or there may be a possibility of error in your case

Hope this helps you.

0
source

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


All Articles