2013-07-15 39 views
4

此功能啓動語音識別,但超時過早,好像語音識別 從IME鍵盤(例如Google鍵盤)啓動,它不會如此快速地超時。 我需要一種方式來啓動Google鍵盤所使用的相同意圖。語音識別超時時間過早android

public void StartSpeechRecognitionActivity(){ 
    try{ 
     Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
     intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass().getPackage().getName()); 
     intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
     intent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true); 
     intent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS, 3000); 
     intent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS, 3000); 
     main.startActivityForResult(intent, SPEECHRECOGNITION_RESULTCODE); 
    } catch (ActivityNotFoundException error) { 
     ShowAndSpeakMessage("Speech recognition is not supported by your device"); 
    } catch(RuntimeException error) { 
     Log.e(TAG, ERROR_PREFIX + Errors.toString(error)); 
    } catch(Error error) { 
     Log.e(TAG, ERROR_PREFIX + Errors.toString(error)); 
     throw error; 
    } 
} 

回答

1
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, 
          "Voice recognition!"); 
startActivityForResult(intent, REQUEST_CODE); 

這只是正常工作對我!

+0

沒有它不是實際上我想,這意圖超時幾秒後,而我想意圖將打開,直到用戶取消 –

0

然後我認爲你需要檢查onError函數。我是這樣做的以下方式。它對我來說工作得很好。

public void onError(int error) { 
    String errorMessage = getErrorText(error); 
    Log.d(LOG_TAG+">"+ "FAILED " + errorMessage); 
    if(errorMessage.contains("RecognitionService busy")) 
    { speech.stopListening(); 
    speech.startListening(recognizerIntent); 


    }else if(errorMessage.contains("No speech input")){ 
     speech.stopListening(); 
     speech.startListening(recognizerIntent); 

    }else if(errorMessage.contains("No match")){ 
     speech.stopListening(); 

     speech.startListening(recognizerIntent); 
    } 

} 

這樣可以使盡可能多的檢查,儘可能多的有如下類型的錯誤,或者有可能是你的情況發生錯誤的可能性

我希望它會幫助你