Android SpeechRecognizer won't start again
I know how SpeechRecognizer works in android. I have a requirement that after a while I need to call the SpeechRecognizer.stopListening () method . But after that, when I started the listener again, it didn't work.
Code to launch SpeechRecognizer
private void promptSpeechInput() {
/*
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
getString(R.string.speech_prompt));
try {
startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
*/
speechIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
speechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
speechIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, context.getPackageName());
// for more: https://stackoverflow.com/questions/7973023/what-is-the-list-of-supported-languages-locales-on-android
/*intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en_IN"); */
speechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
speechIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
speechIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS, 200);
speechIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS, new Long(5000));
speechIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS, new Long(5000));
speechIntent.putExtra("android.speech.extra.DICTATION_MODE", false);
speechIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, false);
/*
intent.putExtra("android.speech.extra.GET_AUDIO_FORMAT", "audio/AMR");
intent.putExtra("android.speech.extra.GET_AUDIO", true);
*/
sr.startListening(speechIntent);
}
After a few seconds of talking, I use
sr.stopListening();
And after getting the results in onResults , I try to run it again using
sr.startListening(speechIntent);
but doesn't work. What should I do to make it work?
EDIT
if i call promptSpeechInput () method again; instead of sr.startListening (speechIntent); it starts giving me the SpeechRecognizer.ERROR_RECOGNIZER_BUSY
error again.
source to share
You will need to install the RecognitionListener again for this to work. Oddly enough, every time you have to listen to the user reset and set the listener again to make it work.
If you are looking for a quick solution, you can try DroidSpeech , which takes care of all heavy flights. With a few lines of code, you will be able to quickly recognize speech recognition.
DroidSpeech droidSpeech = new DroidSpeech(this, null);
droidSpeech.setOnDroidSpeechListener(this);
To start listening to the called user call,
droidSpeech.startDroidSpeechRecognition();
And you will get the result of the voice in the listener method,
@Override
public void onDroidSpeechFinalResult(String finalSpeechResult, boolean droidSpeechWillListen)
{
// Do whatever you want with the speech result
}
source to share