我偶然发现了这个随机问题......这是我的代码
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(mContext);
initializeRecognitionListener();
mSpeechRecognizer.setRecognitionListener(mRecognitionListener);
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass().getPackage().getName());
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");
intent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS, Long.valueOf(3000L));
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 5);
mSpeechRecognizer.startListening(intent);
方法initializeRecognitionListener():
private void initializeRecognitionListener() {
mRecognitionListener = new RecognitionListener() {
@Override
public void onReadyForSpeech(Bundle params) {
Log.d("onReadyForSpeech()", "onReadyForSpeech!");
isRecognizing = true;
}
@Override
public void onBeginningOfSpeech() {
Log.d("onBeginningOfSpeech()", "onBeginningOfSpeech!");
}
@Override
public void onEndOfSpeech() {
Log.e("onEndOfSpeech()", "onEndOfSpeech! stop SCO");
}
...
}
主要问题是“onReadyForSpeech()”和“onBeginningOfSpeech()”方法有时不会在 mSpeechRecognizer.startListening(intent) 之后调用。也不能调用“onEndOfSpeech()”。
我正在使用带有 Android 4.2.2 的 Nexus 4