2017-08-01 43 views
3

我知道SpeechRecognizer如何在android中工作。我有一個要求,我需要在一段時間後致電SpeechRecognizer.stopListening()方法。但之後,當我再次啓動監聽器時,它將無法工作。Android SpeechRecognizer沒有重新開始

代碼開始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); 

但它不工作。我該怎麼做才能使它工作?

EDIT

如果我叫promptSpeechInput();方法而不是sr.startListening(speechIntent);它開始給我SpeechRecognizer.ERROR_RECOGNIZER_BUSY錯誤重複。

回答

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 
}