我知道SpeechRecognizer在android中是如何工作的。我要求我需要在一段时间后调用 SpeechRecognizer.stopListening()方法。但在那之后,当我再次开始倾听时,它将无法工作。
启动SpeechRecognizer的代码
private void promptSpeechInput() {
/*
Intent 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());
intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
getString(R.string.speech_prompt));
try {
startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
*/
speechIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
speechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
speechIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, context.getPackageName());
// for more: https://stackoverflow.com/questions/7973023/what-is-the-list-of-supported-languages-locales-on-android
/*intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en_IN"); */
speechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
speechIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
speechIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS, 200);
speechIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS, new Long(5000));
speechIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS, new Long(5000));
speechIntent.putExtra("android.speech.extra.DICTATION_MODE", false);
speechIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, false);
/*
intent.putExtra("android.speech.extra.GET_AUDIO_FORMAT", "audio/AMR");
intent.putExtra("android.speech.extra.GET_AUDIO", true);
*/
sr.startListening(speechIntent);
}
说完几秒后,我确实使用了
sr.stopListening();
在 onResults 收到结果后,我试图再次使用
启动它sr.startListening(speechIntent);
但它不起作用。我该怎么做才能让它发挥作用?
修改
如果我再次调用 promptSpeechInput(); 方法而不是 sr.startListening(speechIntent); ,它会重复给我SpeechRecognizer.ERROR_RECOGNIZER_BUSY
错误。
答案 0 :(得分:0)
您需要再次设置 RecognitionListener 才能使其正常工作。听起来很奇怪,每当你需要听取用户重置并再次设置监听器以使其工作时。
如果您正在寻找快速解决方案,可以尝试DroidSpeech,这可以解决所有繁重的问题。只需几行代码,您就可以立即完成语音识别。
DroidSpeech droidSpeech = new DroidSpeech(this, null);
droidSpeech.setOnDroidSpeechListener(this);
要开始收听用户,请拨打以下代码,
droidSpeech.startDroidSpeechRecognition();
您将在侦听器方法中获得语音结果
@Override
public void onDroidSpeechFinalResult(String finalSpeechResult, boolean droidSpeechWillListen)
{
// Do whatever you want with the speech result
}