Android - 语音识别限制听力时间

ysf*_*yln 6 android speech-recognition recognizer-intent

我使用Google API进行语音识别,但希望限制收听时间.例如两秒钟.两秒钟后,即使用户继续说话,识别器仍应停止收听.我尝试了一些类似的EXTRA

EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS

但它没有帮助我.我的完整代码在这里,如果有人可以帮助我,我将不胜感激

public void promptSpeechInput()
{
    //This intent recognize the peech
    Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    i.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
    i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Say Something");

    try {
        startActivityForResult(i, 100);
    }
    catch (ActivityNotFoundException a)
    {
        Toast.makeText(MainActivity.this,"Your device does not support",Toast.LENGTH_LONG).show();
    }
}

//For receiving speech input
public void onActivityResult(int request_code, int result_code, Intent i)
{
    super.onActivityResult(request_code, result_code, i);

    switch (request_code)
    {
        case 100: if(result_code == RESULT_OK && i != null)
        {
            ArrayList<String> result = i.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            resultTEXT.setText(result.get(0));
        }
            break;
    }
}
Run Code Online (Sandbox Code Playgroud)

Fra*_*cia 3

您无法限制识别器侦听的时间。只需设置他在关闭前需要收听的最短时间,而不是最长时间。

我也一直在寻找解决这个问题的方法,所以我希望你能找到更好的解决方案。我从另一个 StackOverflow 伙伴那里找到了这篇文章:

语音识别器时间限制

在那里,他提出了解决你的问题的下一个可能性:

最好的选择是线程化某种计时器,例如 CountDownTimer:

 yourSpeechListener.startListening(yourRecognizerIntent);
 new CountDownTimer(2000, 1000) {

 public void onTick(long millisUntilFinished) {
     //do nothing, just let it tick
 }

 public void onFinish() {
     yourSpeechListener.stopListening();
 }   }.start();
Run Code Online (Sandbox Code Playgroud)

换句话说,为了使 SpeechRecognition 简短,您可以将下一个参数添加到您的 Intent 中: EXTRA_PARTIAL_RESULTS

这将使您从 SpeechRecognizer 获得部分结果,这意味着您的方法onActivityPartialResult将返回另一个具有匹配值的数组。该方法在onActivityResults之前调用,速度更快,但当然不如onActivityResult精确。因此,如果您的听众正在寻找特定的单词,这将对您有所帮助。