【问题标题】:Android SpeechRecognizer not getting started againAndroid SpeechRecognizer 没有重新开始
【发布时间】:2018-01-08 05:44:10
【问题描述】:

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

【问题讨论】:

    标签: android kotlin speech-recognition


    【解决方案1】:

    您需要再次设置 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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-11
      • 1970-01-01
      • 1970-01-01
      • 2016-11-27
      • 1970-01-01
      相关资源
      最近更新 更多