簡體   English   中英

如何以編程方式關閉 Android Speech To Text 對話框

[英]How to dismiss the Android Speech To Text dialog box Programmatically

我是安卓編程的新手。 目前,我正在 android 中研究 Speech To Text。 如果用戶不說話,我想以編程方式關閉語音輸入提示。

我該怎么做?

這是代碼。

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)

您可以在 startSpeech 方法中添加以下行之一或兩者:

intent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS,10); 

intent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS,10); 

這是兩個常量之間的區別是:

EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS :我們停止聽到語音后認為輸入完成應該花費的時間。

EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS :我們停止聽到語音后考慮輸入可能完成所需的時間。

注意:這里 10 指定,10 毫秒。 您可以根據自己的方便逐個更改值。

通過使用語音識別器類,我能夠擺脫這個問題。 這是鏈接https://developer.android.com/reference/android/speech/SpeechRecognizer

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM