2015年3月17日 星期二

[Android] Google語音辨識 - 自己實作UI

[Android] Google語音辨識 - 使用RecognizerIntent裡,
我們使用了RecognizerIntent來執行語音辨識,
但是,如果我們想要有自己特色的UI呢?

我們還另外需要SpeechRecognizer的幫忙
Intent recognizerIntent;
private SpeechRecognizer sr;

sr = SpeechRecognizer.createSpeechRecognizer(this);       
sr.setRecognitionListener(new listener());        
recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);        
recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,5);//5是你需要回傳多少個結果 

//開始辨識,將此code移到某個按鈕的onClick()裡
sr.startListening(recognizerIntent);  

//停止辨識,將此code移到某個按鈕的onClick()裡
//我們平常不需要做這個處理,語音辨識完畢它自己會停
//但如果我們想中斷它,或者有異常發生時,就可以呼叫底下的程式碼
sr.stopListening();
sr.cancel();
上面的listener(),代碼如下:
private static final int RECOGNIZER_ERROR_NETWORK_TIMEOUT = 1;//網路超時
private static final int RECOGNIZER_ERROR_NETWORK = 2;//其它的網路相關錯誤
private static final int RECOGNIZER_ERROR_AUDIO = 3;//無法錄音
private static final int RECOGNIZER_ERROR_SERVER = 4;//伺服器異常
private static final int RECOGNIZER_ERROR_CLIENT = 5;//其它手機端的異常
private static final int RECOGNIZER_ERROR_SPEECH_TIMEOUT = 6;//沒有語音輸入
private static final int RECOGNIZER_ERROR_NO_MATCH = 7;//沒有符合的辨識結果
private static final int RECOGNIZER_ERROR_RECOGNIZER_BUSY = 8;//RecognitionService 忙碌中
private static final int RECOGNIZER_ERROR_INSUFFICIENT_PERMISSIONS = 9;//權限不足

class listener implements RecognitionListener          
{
    public void onReadyForSpeech(Bundle params)
    { 
      //已經準備好可以接受語音輸入,可以在此時通知UI秀出相關icon,告知使用者開始說話             
    }
    public void onBeginningOfSpeech()
    {  
      //使用者開始說話,可以在此時通知UI秀出相關icon,告知使用者已收到聲音輸入了        
    }
    public void onRmsChanged(float rmsdB)
    { 
      //聲音輸入的強度變化,值是0~10
      //可以在此時通知UI秀出聲音大小的相關icon
      //onRmsChanged()來的非常頻繁和快速,最好是判斷有變化才通知UI更新
    }
    public void onBufferReceived(byte[] buffer)
    {       
    }
    public void onEndOfSpeech()
    {
       //使用者結束說話,系統會開始進行聲音辨識
    }
    public void onError(int error)
    {
       switch(error)
       {
        case RECOGNIZER_ERROR_NETWORK_TIMEOUT:         
         break;
        case RECOGNIZER_ERROR_NETWORK:         
         break;
        case RECOGNIZER_ERROR_AUDIO:         
         break;
        case RECOGNIZER_ERROR_SERVER:         
         break;
        case RECOGNIZER_ERROR_CLIENT:         
         break;
        case RECOGNIZER_ERROR_SPEECH_TIMEOUT:         
         break;
        case RECOGNIZER_ERROR_NO_MATCH:         
         break;
        case RECOGNIZER_ERROR_RECOGNIZER_BUSY:         
         break;
        case RECOGNIZER_ERROR_INSUFFICIENT_PERMISSIONS:         
         break;
       }
    }
    public void onResults(Bundle results)                   
    {
        //辨識的結果
        ArrayList data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);         
        String firstMatched = (String) data.get(0);         
        txtResult.setText(firstMatched);
    }
    public void onPartialResults(Bundle partialResults)
    {
    }
    public void onEvent(int eventType, Bundle params)
    {
    }
}

沒有留言:

張貼留言