简体   繁体   中英

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. 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 :

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM