SpeechRecognizer - 时间限制

lit*_*dev 3 android speech-recognition voice-recognition

我正在使用SppechRecognizer进行语音识别器应用程序.它的工作正常.我的要求是我希望在1秒或2秒后停止语音收听.怎么实现呢?

Otr*_*tra 9

1或2秒似乎没有很多时间,但如果你想设置一个时间限制,你可能需要线程化它.Android有一些默认的额外设置来设置语音输入的最小长度和用户停止讲话后的最大数量,但没有设置语音输入的最大时间长度.

你最好的选择是通过某种计时器,比如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)

我还鼓励您查看RecognizerIntent可用的额外内容,看看是否有更适合您需求的内容.