[英]How to dismiss the Android Speech To Text dialog box Programmatically
I am new to android programming.我是安卓编程的新手。 Currently, I am working on Speech To Text in android.
目前,我正在 android 中研究 Speech To Text。 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;
}
}
}
只是使用
finishActivity(REQ_CODE_SPEECH_INPUT);
您可以通过几秒钟的延迟关闭并调用finishActivity(84)
you can add the one of the following line or both the inside the startSpeech method :您可以在 startSpeech 方法中添加以下行之一或两者:
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_COMPLETE_SILENCE_LENGTH_MILLIS :我们停止听到语音后认为输入完成应该花费的时间。
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. EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS :我们停止听到语音后考虑输入可能完成所需的时间。
Note : here 10 specifies, 10 ms.注意:这里 10 指定,10 毫秒。 you can change the value by value by your own convenience.
您可以根据自己的方便逐个更改值。
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
这是链接https://developer.android.com/reference/android/speech/SpeechRecognizer
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.