语音识别器 |什么都没说之后发生了什么(什么都没发生)?

SpeechRecognizer | What happend (nothing happend) after say nothing?

我在 google 上找不到答案,或者我没有找到合适的词。

所以 SpeechRecognizer 工作正常。 但是当我听到哔哔声(我在没有 google 对话框的情况下使用它)并且我在大约 3 秒或更长时间内什么也没说时,它就像识别器什么都不做并逐渐消失,没有听到第二声哔哔声,没有 onResult() , 没有 EndofSpeech。

那么当识别器正在倾听而你什么也没说时发生了什么?哪种方法被触发?

编辑:毕竟它有效,非常感谢 OpiateFuchs 和他非常好的评论和回答。我这样编辑简化代码,你们可以看看如何制作它。
即使你什么也没说, onPartialResult() 也会经常被调用,但当这种情况发生时,partialResult 字符串是空的,所以如果它是空的,你就知道什么也没说。 (来自 OpiateFuchs 的创意)

这就是我对识别器很重要的简化代码:

public Constructor (){
        speech = SpeechRecognizer.createSpeechRecognizer(context);
        speech.setRecognitionListener(this);
        recogIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        recogIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, "de");
        speech.startListening();
}

public void startListening(){
    speech.startListening(recogIntent);
    if(timerRunning==false){
        setcdt();
        mCountDownTimer.start();
        timerRunning=true;
    }
}

@Override
public void onReadyForSpeech(Bundle params) {

}

@Override
public void onBeginningOfSpeech() {

}

@Override
public void onRmsChanged(float rmsdB) {

}

@Override
public void onBufferReceived(byte[] buffer) {

}

@Override
public void onEndOfSpeech() {
    Toast.makeText(c, "work", Toast.LENGTH_SHORT);
    //too see if its called
}

@Override
public void onError(int error) {

}

@Override
public void onResults(Bundle results) {
    ArrayList<String> matches = results
            .getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);

    Toast.makeText(c, matches.get(0), Toast.LENGTH_LONG).show();
    speech.cancel();
    analyse(matches.get(0));
    m.next(); //calls the Recognizer again

}

@Override
public void onPartialResults(Bundle partialResults) {
ArrayList<String> matches = partialResults
            .getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);

    if(matches.get(0).length() == 0){
        m.tv.append("nothing"); //show on textview

    }
    else{
        m.tv.append("cancel timer "); //show on textview
        mCountDownTimer.cancel();
        hasSpoken = true;
        timerRunning = false;
    }

}

@Override
public void onEvent(int eventType, Bundle params) {

}

//innerclass
public class FinishSpeechRecognizerTimer extends CountDownTimer {

    public FinishSpeechRecognizerTimer(long startTime, long interval){
        super(startTime,interval);

    }

    @Override
    public void onFinish(){
        timerRunning = false;
        if (hasSpoken==false){
            m.tv.append("nospeak ");
            speech.cancel();
            m.tv.append("listen again after no speak ");
            startListening();
        }
    }

    @Override
    public void onTick(long millisUntilFinish){

    }
}

public void setcdt(){
    startTime = 5000;
    interval = 4000; //want no onTick - interval > startTime
    timerRunning = false;
    hasSpoken = false;
    mCountDownTimer = new FinishSpeechRecognizerTimer(startTime, interval);
}

对于本次讨论,我尝试提供一个从头开始的示例,它将引导您走向正确的方向。正如评论中所说,如果什么都不说, SpeechRecognition 似乎不会自行停止,因此您可以 implement a CountDownTimer 例如并完成 SpeechRecognizer一段时间后:

全局化 SpeechRecognizer(就像你所做的那样),booleanCountdownTimer objects :

private SpeechRecognizer speech;   
private boolean hasSpoken=false;
private CountDownTimer mCountDownTimer;
private long startTime = 30000L;
private long interval = 1000L;
private boolean timerRunning = false;

扩展 CountDownTimer class:

public class FinishSpeechRecognizerTimer extends CountDownTimer{

        public FinishSpeechRecognizerTimer(long startTime, long interval){
          super(startTime,interval);

           }

         @Override
         public void onFinish(){

             if(hasSpoken==false){
                speech.cancel();
            }

           timerRunning=false;

          }

         @Override
         public void onTick(long millisUntilFinish){

           //do whatever you want to do

       }
}

初始化

speech = SpeechRecognizer.createSpeechRecognizer(yourContext);
mCountDownTimer = new FinishSpeechRecognizerTimer(startTime, interval);

启动它的同时启动CountDownTimer:

speech.startListening(recogIntent);
if(timerRunning==false){
mCountDownTimer.start();
timerRunning=true;
}

一旦有人说话,将 boolean value hasSpoken 设置为 truecancel 计时器:

@Override
public void onBeginningOfSpeech() {
   hasSpoken=true;
   mCountDownTimer.cancel();
   timerRunning=false;
}

正如我所说,它是从头开始的,不能保证它能正常工作。此示例启动 CountDownTimer 30 秒,并每秒检查是否有人说话。你想等多久由你决定。

编辑

事实证明,在某些情况下,onBeginOfSpeech() 方法被多次调用而没有人说话。对于所有感兴趣的人:

您可以使用方法 onPartialResult():

而不是在 onBeginOfSpeech() 中执行那些操作
@Override
public void onPartialResults(Bundle partialResults) {
ArrayList<String> matches = results
            .getStringArrayList(SpeechRecognizer.RESULT_RECOGNITION);
 if(matches.size()==0){

        hasSpoken=false;

 }else{

  hasSpoken=true;
  mCountDownTimer.cancel();
       timerRunning=false;
   }
}