我目前正在使用不带对话框的使用语音识别的应用程序。为此,我使用了自定义的RecognitionListener实现。到目前为止,一切工作都很好,除了一件事:在调用“ startListening”方法之后,在出现超时错误之前开始讲话的时间非常短。基本上,我希望应用程序在完全安静的状态下等待更长的时间,因为此刻大约需要1秒钟。
这是我的代码:
speechRec = SpeechRecognizer.createSpeechRecognizer(this);
speechRec.setRecognitionListener(this);
intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
//on click
speechRec.startListening(intent);
//overridden methods
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQ_CODE_SPEECH_INPUT: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
txt2.setText(result.get(0));
}
break;
}
}
}
@Override
public void onReadyForSpeech(Bundle arg0){
Log.i(LOG_TAG, "onReadyForSpeech");
}
@Override
public void onBeginningOfSpeech(){
Log.i(LOG_TAG, "onBeginningOfSpeech");
}
@Override
public void onEndOfSpeech(){
Log.i(LOG_TAG, "onEndOfSpeech");
}
@Override
public void onBufferReceived(byte[] buffer){
Log.i(LOG_TAG, "onBufferReceived: " + buffer);
}
@Override
public void onResults(Bundle results){
Log.i(LOG_TAG, "onResults");
ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
String text = "";
for (String result : matches)
text += result + "\n";
txt2.setText(text);
}
@Override
public void onError(int errCode){
String error = getError(errCode);
Log.d(LOG_TAG, "ERROR: " + error);
ttobj.speak(error, TextToSpeech.QUEUE_FLUSH, null);
}
@Override
public void onRmsChanged(float rmsdB){
Log.i(LOG_TAG, "onRmsChanged: " + rmsdB);
}
@Override
public void onEvent(int arg0, Bundle arg1){
Log.i(LOG_TAG, "onEvent");
}
@Override
public void onPartialResults(Bundle arg0){
Log.i(LOG_TAG, "onPartialResults");
}
我已经尝试过进行此操作,但没有帮助:
intent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS, 2000);
intent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS, 2000);
intent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS, 15000);
那么,有什么方法可以迫使语音识别器在用户开始讲话之前等待更长的时间,而不必再次开始录音?
答案 0 :(得分:0)
再次在onResults和onError方法中启动侦听器。