gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:25:39 24 4
gpt4 key购买 nike

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

所以 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);
}

最佳答案

对于本次讨论,我尝试提供一个从头开始的示例,它将引导您走向正确的方向。正如评论中所说,似乎 SpeechRecognition如果什么都不说,它不会自己停下来,所以你可以implement一个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 hasSpokentruecancel计时器:

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

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

编辑

事实证明,在某些情况下,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;
}
}

关于java - 语音识别器 |什么都没说之后发生了什么(什么都没发生)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36453120/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com