2012-06-28 82 views
3

我正在開發一款總是監聽用戶語音的Android應用程序。它在Sony X10i上運行時很有用,但在Samsung Galaxy SII中不起作用。 這裏是我的代碼:語音識別監聽器在Galaxy SII中不起作用

SpeechRecognizer  speechRecognizer; 
    speechRecognizer = SpeechRecognizer.createSpeechRecognizer(getBaseContext()); 
    MyRecognitionListener speechListner=new MyRecognitionListener(); 
    speechRecognizer.setRecognitionListener(speechListner); 
    speechRecognizer.startListening(RecognizerIntent.getVoiceDetailsIntent(getApplicationContext())); 

這裏是我的監聽器類:

class MyRecognitionListener implements RecognitionListener { 

    public void onBeginningOfSpeech() { 
     Log.d("leapkh", "onBeginningOfSpeech"); 
    } 

    public void onBufferReceived(byte[] buffer) { 
     Log.d("leapkh", "onBufferReceived"); 
    } 

    public void onEndOfSpeech() { 
     Log.d("leapkh", "onEndOfSpeech"); 
    } 

    public void onError(int error) { 
     Log.d("leapkh", "onError"); 
    } 

    public void onEvent(int eventType, Bundle params) { 
     Log.d("leapkh", "onEvent"); 
    } 

    public void onPartialResults(Bundle partialResults) { 
     Log.d("leapkh", "onPartialResults"); 
    } 

    public void onReadyForSpeech(Bundle params) { 
     Log.d("leapkh", "onReadyForSpeech"); 
    } 


    public void onResults(Bundle results) { 
     Log.d("leapkh", "onResults"); 

    } 

    public void onRmsChanged(float rmsdB) { 
     Log.d("leapkh", "onRmsChanged"); 
    } 
} 

在這種情況下,如何解決這個問題?

回答

7

我找到了解決方案。

變化speechRecognizer.startListening()方法來intent如下參數:

Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
    intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getApplication().getPackageName()); 
    speechRecognizer.startListening(intent); 
0

更改您傳遞的意圖

{ 
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 

intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, "en"); 

intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, 
       this.getPackageName()); 

intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, 
       RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH); 

intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3); 

    if (speech != null) { 
      speech = null; 

     } 

     SpeechRecognizer speech SpeechRecognizer.createSpeechRecognizer(this); 

     speech.setRecognitionListener(this); 

     speech.startListening(intent); 

}

另外的參數檢查錯誤的哪種類型的你正在獲取 for nomatch,網絡和服務器錯誤調用startListening再次

public void startListening() { 
    try { 

     if (SpeechRecognizer.isRecognitionAvailable(this)) { 
      if (speech != null) { 
       speech.startListening(intent); 

      } else { 
       SimpleMethod(); 
      } 
     } 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
}