Reputation: 708
I am new to android programming. Currently, I am working on Speech To Text in android. I would like to dismiss the speech input prompt programmatically if user does not speak anything.
How am I supposed to do that?
Here is the code.
public void startSpeech(View view) {
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,
"Speak something");
try {
startActivityForResult(intent, 84);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
"Speech is currently not supported",
Toast.LENGTH_LONG).show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 84: {
if (resultCode == RESULT_OK && data != null) {
ArrayList<String> result = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
// The spoken words...
}
// if user did not speak anything, then close the dialog.
// ???
break;
}
}
}
Upvotes: 4
Views: 1364
Reputation: 708
I was able to get rid of this issue by using the speech recognizer class. Here's the link https://developer.android.com/reference/android/speech/SpeechRecognizer
Upvotes: 0
Reputation: 3469
you can add the one of the following line or both the inside the startSpeech method :
intent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS,10);
or
intent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS,10);
this is the difference between both the constant is :
EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS : The amount of time that it should take after we stop hearing speech to consider the input complete.
EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS : The amount of time that it should take after we stop hearing speech to consider the input possibly complete.
Note : here 10 specifies, 10 ms. you can change the value by value by your own convenience.
Upvotes: 0
Reputation: 9
You can close by the delay of few seconds and calling finishActivity(84)
Upvotes: 0