我正在Android中使用SpeechRecognizer和RecognizerIntent来实现语音识别.我的目的是在语音识别器在屏幕上显示结果后重新开始收听语音.为此,我使用以下代码.
问题是,第一次运行正常并显示结果,但是在第二次开始监听之后(从onResults方法调用),由于某种原因,它听不到声音.然后,它给出一个ERROR_SPEECH_TIMEOUT错误,这意味着没有语音输入.在Logcat上,我可以看到它进入onReadyForSpeech(),但不知何故,它听不到我在说什么.
有谁知道为什么会这样?返回结果后是否继续监听?再次显式调用startListening是否正确?
public class VR extends Activity implements RecognitionListener {
private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;
private TextView vrtext;
private SpeechRecognizer speech = null;
private Intent intent;
private String TAG = "VR";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.vr);
vrtext = (TextView) findViewById(R.id.vrtext);
}
@Override
public void onResume()
{
listen();
super.onResume();
}
private void listen()
{
speech = SpeechRecognizer.createSpeechRecognizer(this);
speech.setRecognitionListener(this);
intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, "en");
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, this.getPackageName());
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3);
speech.startListening(intent);
}
@Override
protected void onPause() {
super.onPause();
// TODO Auto-generated method stub
if(speech != null)
{
speech.destroy();
Log.i(TAG,"destroy");
}
}
public void onBeginningOfSpeech() {
// TODO Auto-generated method stub
Log.i(TAG, "onbeginningofspeech");
}
public void onBufferReceived(byte[] arg0) {
// TODO Auto-generated method stub
//Log.i(TAG, "onbufferreceived");
}
public void onEndOfSpeech() {
// TODO Auto-generated method stub
Log.i(TAG, "onendofspeech");
}
public void one rror(int arg0) {
// TODO Auto-generated method stub
Log.i(TAG, "error code: " + arg0);
}
public void onEvent(int arg0, Bundle arg1) {
// TODO Auto-generated method stub
Log.i(TAG, "onevent");
}
public void onPartialResults(Bundle arg0) {
// TODO Auto-generated method stub
Log.i(TAG, "onpartialresults");
}
public void onReadyForSpeech(Bundle arg0) {
// TODO Auto-generated method stub
Log.i(TAG, "onreadyforspeech");
}
public void onResults(Bundle arg0) {
// TODO Auto-generated method stub
Log.i(TAG, "onresults");
ArrayList<String> matches = arg0.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
String s = "";
for (String result:matches)
s += result + "\n";
vrtext.setText(s);
speech.startListening(intent);
}
public void onRmsChanged(float arg0) {
// TODO Auto-generated method stub
//Log.i(TAG, "onrmschanged");
}
}
解决方法:
“返回结果后,它继续听吗?”
没有
“再次显式调用startListening是否正确?”
是.
另外,如果您希望继续进行识别,则在发生以下错误时应再次调用startListening:
@Override
public void one rror(int errorCode)
{
if ((errorCode == SpeechRecognizer.ERROR_NO_MATCH)
|| (errorCode == SpeechRecognizer.ERROR_SPEECH_TIMEOUT))
{
Log.d(TAG, "didn't recognize anything");
// keep going
recognizeSpeechDirectly();
}
else
{
Log.d(TAG,
"FAILED "
+ SpeechRecognitionUtil
.diagnoseErrorCode(errorCode));
}
}
检出我的代码,以使用SpeechRecognizer来检测某个口语单词here.