2013-03-16 65 views
6

我提高其使用RecognitionListener類傾聽用戶語音的Android應用程序後再次,在這裏我得到下面的結果:在麥克風圖標如何重新啓動監聽RecognitionListener採取ERROR_RECOGNIZER_BUSY錯誤

1)如果用戶點擊說了句一切都很好 2-)如果麥克風圖標,用戶點擊再次點擊麥克風圖標或沒有說什麼,我得到的onerror和錯誤類型是:ERROR_RECOGNIZER_BUSY

@Override 
public void onError(int error) { 
if ((error == SpeechRecognizer.ERROR_NO_MATCH) 
    || (error == SpeechRecognizer.ERROR_SPEECH_TIMEOUT)){ 

    } 
    else if(ERROR_RECOGNIZER_BUSY){ 
    } 

} 

這裏是我的代碼爲首發名單效果圖創作:

public void recognizeSpeechDirectly() 
    { 


     recognizer = SpeechRecognizer.createSpeechRecognizer(this.context); 
     recognizer.setRecognitionListener(this); 
     recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
     recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
     recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, "org.twodee.andytest"); 
     recognizerIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true); 
     recognizer.startListening(recognizerIntent); 

    } 

我想ERROR_RECOGNIZER_BUSY出現時重新啓動監聽,

另一個傢伙告訴計算器上的這個錯誤,但目前尚不清楚,我和無法實現它。

How to handle ERROR_RECOGNIZER_BUSY

在此先感謝

回答

0

你有ERROR_RECOGNIZER_BUSY,因爲你叫startListening時兩次用戶點擊該按鈕並再次點擊。更改您的代碼如下:

// class member 
private boolean mIsListening; 
@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    ......... 
    recognizer = SpeechRecognizer.createSpeechRecognizer(this.context); 
    recognizer.setRecognitionListener(this); 
    recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, "org.twodee.andytest"); 
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true); 
} 

,並點擊

if (!mIslistening) 
{ 
    mIsListening = true;   
    recognizer.startListening(recognizerIntent); 
} 

@Override 
public void onError(int error) { 
if ((error == SpeechRecognizer.ERROR_NO_MATCH) 
    || (error == SpeechRecognizer.ERROR_SPEECH_TIMEOUT)){ 

    } 
    else if(ERROR_RECOGNIZER_BUSY){ 

    } 
    recognizer.startListening(recognizerIntent); 
} 

@Override 
    public void onPartialResults(Bundle partialResults) 
    { 
     mIsListening = false; 
     .......... 
    } 

@Override 
    public void onResults(Bundle results) 
    { 
     mIsListening = false; 
      .......... 
    } 
+1

感謝回覆的還,但是當我取消recongnizer和重啓**彷彿在聽(ERROR_RECOGNIZER_BUSY) **,而不是重新開始聽同樣的錯誤在無限循環中重複。 – odincer 2013-03-17 11:39:59

+0

我編輯了我的答案。 – 2013-03-17 17:55:19

+0

你好,我當然應用你說的,但它不適合我,你有任何工作示例?我的機器是三星S3與android 4.1順便說一句。 – odincer 2013-03-17 19:34:58

0

圖標時啓動的認可:recognizeSpeechDirectly();

public void stopRecognition(){ 
     recognizer.destroy(); 
     recognizer = null; 

} 

public void onError(int error) { 
     stopRecognition(); 
} 

public void onResults(Bundle results){ 
     //Do something 
     stopRecognition(); 
} 

它的工作原理來修復「沒有連接到識別服務」「ERROR_RECOGNIZER_BUSY」錯誤