android語音識別 android.speech 包分析

 

 

 

 

android voice recognition,語音識別作爲Service實現。 語音識別API在android.speech中 API大致分成應用端API和service端API

 

RecognizerIntent

顧名思義,在自己的程序中,需要語音識別了,發出這個intent,如果已經安裝了google voice search應用,谷歌的activity,service就會跳出來幫你。 http://download.csdn.net/source/2591401有三個例子。 例子1基本就實現了這樣一個應用。 例子1實際上很簡單,就靠RecognizerIntent,intent發出後,相應的service會處理intent。 基本上是借鑑了google官方sample:http://developer.android.com/resources/articles/speech-input.html 和 http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/VoiceRecognition.html

(運行例子1的前提是,google voice search應用已經安裝,這樣確保Service已經安裝了) 注意,需要微調程序中的RecognizerIntent的三個參數,可以得到不同的運行效果,簡單,參考javadoc即可。 

 

  1. Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);  

  2. intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, 
  3.  
  4. RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 

 

以上是最簡單的方式,只需要RecognizerIntent一個類就搞定

 

自己實現RecognitionService 

 

如果要自己做一個識別引擎Service,是可以的。Android提供了RecognitionService抽象類  識別引擎作爲Service存在,要實現這個抽象類.  This RecognitionService class should be extended only in case you wish to implement a new speech recognizer.  

Android SDK提供了一個sample VoiceRecognitionService extends RecognitionService,是一個假的引擎,識別結果固定爲abc或者123。 這個引擎不讀取麥克風,也不含麥克風dialog界面。稍候在SpeechRecognizer處介紹如何使用這個假service. 

 

RecognitionService比較樸素,提供三個方法: 

  1. abstract void   onStartListening(Intent recognizerIntent, RecognitionService.Callback listener)  
  2. Notifies the service that it should start listening for speech. 開始識別,intent發過來了 
  3.  
  4. abstract void   onStopListening(RecognitionService.Callback listener) 
  5. Notifies the service that it should stop listening for speech. 結束識別 
  6.  
  7. abstract void   onCancel(RecognitionService.Callback listener)  
  8. Notifies the service that it should cancel the speech recognition. 取消識別 

 RecognitionService是無狀態的,不能並行開始識別多個音頻流,三個方法必須按照開始-->結束/取消,再開始-->結束/取消,這樣順序調用,否則報錯。

 

 

谷歌的Service實現,不開源

AOSP裏面沒有實現RecognitionService的代碼。所以默認的AOSP編出來的android SDK都是不帶語言識別Service的,SDK桌面的google search widget沒有麥克風,就說明識別Service還沒裝。

google自己實現了RecognitionService,裝了voicesearch 2.1.4之後(一般手機都已經預置),桌面的google search widget也會出現小麥克了點擊小麥克出現"speak now"對話框。同時settings voice input output的識別引擎選擇框會出現google的選項。谷歌的service叫com.google.android.voicesearch.RecognitionService 這個程序也不開源,無法深度定製谷歌那個"speak now"對話框。

 

P.S. google的識別是基於網絡的。 google voice search小應用client不開源,網絡端更不開源,網絡API尚未開放,但有些人已經研究出來了...  refer to: 

http://stackoverflow.com/questions/2080401/is-there-a-speech-to-text-api-by-google

https://github.com/taf2/speech2text 

https://github.com/taf2/audiosplit 

http://mikepultz.com/2011/03/accessing-google-speech-api-chrome-11 比較牛

vlingo virtual assistant也實現了RecognitionService,不開源

 

 

RecognitionService.Callback 的作用

 

上面三個方法都有一個參數是Callback類形。callback類是個二傳手,被service調用,並通過ipc轉發結果給RecognitionListener。

The RecognitionService.Callback class receives callbacks from the RecognitionService and forwards them to the RecognitionListener

RecognitionListener是用戶自己進程中實現的,RecognitionService是在service進程中的,service沒法直接調用listener,就通過callback類轉發。

callback幫助自己做service的人處理了ipc,aidl等問題,挺好的。

 

SpeechRecognizer  使用service的

 

This class provides access to the speech recognition service. 給應用程序進程用的,方便對RecognitionService的訪問

This class's methods must be invoked only from the main application thread. 只有主應用線程中可以調用...爲什麼呢...可能因爲service會彈出識別的dialog...?

Please note that the application must have RECORD_AUDIO permission to use this class. 程序必須有錄音權限

packages/inputmethods/LatinIME/java/src/com/android/inputmethod/voice/VoiceInput.java 可以參考  

例子3  MyStt3Activity也是個參考,但要給識別intent加一行,否則出現error 5

intent.putExtra("calling_package", "");

在例子3中,按下speak按鈕之後,程序用SpeechRecognizer.startListening(intent); 開啓了麥克風等待用戶說話,不會彈出google的“speak now”麥克風的dialog,直接說就行了。

如果此時settings裏面設定默認engine爲demo的com.example.android.voicerecognitionservice假引擎,則乾脆不會讀取麥克風,直接返回abc或者123的假結果。

我也試驗了vlingo virtual assistant的引擎,也可以用。

多個RecognitionService實現並存,是允許的。不通過settings的默認設置,程序化指定Service,怎麼做?todo  intent.setClassName不行

RecognizerResultsIntent 略

 

 

 

 

開源SREC庫,是nuance貢獻給AOSP的

 

利用 srec 裏面的動態插入語音標籤功能( Voice Enrollment ) , 添加事先錄好的普通話短語,之後就可以對其做語音識別了,

例子2 是調用SREC的,可以參考 (由於SREC的API爲@Hide,玩轉例子2需要點小技巧) 

 

 

非google的風格的識別

最後,有的廠商自己搞voice framework,不走google android.speech的任何代碼,從activity到intent到service到widget全都自己設計,也行。超出本文範圍。  

 

 

refer to:

http://blog.csdn.net/lzf_china/article/details/4510980

http://topic.csdn.net/u/20100817/15/bcc84f6f-1fec-4e73-9dbf-9ef595597926.html 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章