1

我在谷歌上找不到答案,或者我没有得到正确的词。

所以 SpeechRecognizer 工作正常。但是当我听到哔哔声(我在没有谷歌对话框的情况下使用它)并且我没有说大约 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);
}
4

1 回答 1

1

对于本次讨论,我尝试从头开始提供一个示例,该示例应引导您走向正确的方向。正如评论中SpeechRecognition所说,如果什么都不说,似乎不会从自身停止,所以你可以简单地例如并在implement一段时间后完成:CountDownTimerSpeechRecognizer

使全局SpeechRecognizer(就像您所做的那样),boolean并且CountdownTimer 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 hasSpokentotruecancel计时器:

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

正如我所说,它是从头开始的,不能保证这是有效的。此示例启动CountDownTimer30 秒,并每秒检查是否有人说话。你想等多久取决于你。

编辑

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

而不是做那些东西onBeginOfSpeech(),你可以使用该方法onPartialResult():

@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;
   }
}
于 2016-04-07T13:11:02.307 回答