我在Google上找不到答案,或者我找不到正确的答案。
所以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);
}
答案 0 :(得分:1)
对于这个讨论,我尝试从头开始提供一个示例,它将引导您进入正确的方向。正如评论中所述,如果没有任何内容,SpeechRecognition
似乎不会自行停止,所以您只需implement
CountDownTimer
例如SpeechRecognizer
完成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
一旦说出某些内容,请将hasSpoken
true
设置为cancel
和@Override
public void onBeginningOfSpeech() {
hasSpoken=true;
mCountDownTimer.cancel();
timerRunning=false;
}
计时器:
CountDownTimer
正如我所说,从头开始,无法保证这是有效的。此示例启动onBeginOfSpeech()
30秒,并检查是否有内容。你想等多久取决于你。
事实证明,在某些情况下,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;
}
}
{{1}}