gpt4 book ai didi

Android SpeechRecognizer 没有重新开始

转载 作者:行者123 更新时间:2023-11-29 02:39:03 25 4
gpt4 key购买 nike

我知道 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 错误反复。

最佳答案

您需要再次设置 RecognitionListener 才能使此工作正常进行。听起来很奇怪,每次当您需要收听用户重置并再次设置收听者以使其工作时。

如果您正在寻找快速解决方案,可以尝试 DroidSpeech ,它负责所有繁重的工作。只需几行代码,您就可以立即完美运行语音识别。

DroidSpeech droidSpeech = new DroidSpeech(this, null);
droidSpeech.setOnDroidSpeechListener(this);

要开始收听用户调用以下代码,

droidSpeech.startDroidSpeechRecognition();

然后你会在listener方法中得到语音结果,

@Override
public void onDroidSpeechFinalResult(String finalSpeechResult, boolean droidSpeechWillListen)
{
// Do whatever you want with the speech result
}

关于Android SpeechRecognizer 没有重新开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45430541/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com