實現Android語音辨識服務介面 RecognitionService的方法

來源:互聯網
上載者:User

之前的一篇文章介紹過SpeechRecognizer類,該類可以作為對外的一個介面,並通過Intent傳遞一個ComponentName擷取可支援語音辨識的功能的服務,一般的使用者手機中如果安裝了語音辨識的一些軟體,就會擁有這樣的能力,但是如果開發人員希望自己通過某公司的sdk整合自己的語音辨識服務,那麼就需要實現RecognitionService這個類。

這個類是個抽象類別,需要開發人員完成其中的幾個抽象方法。下面的代碼注釋中對每個方法進行了說明。

public class BDRecognitionService extends RecognitionService {       @Override    public void onCreate() {        super.onCreate();        // 在調用服務時進行一次    }    @Override    protected void onStartListening(Intent recognizerIntent, Callback mCallback) {      //這個方法具體實現了第三方sdk的接入, //recognizerIntent 攜帶了上層傳入的參數//mCallback 需要將第三方sdk的結果回調給該介面,是上層與第三方sdk的橋樑} @Override protected void onCancel(Callback listener) { //登出 } @Override public void onDestroy() { //銷毀 super.onDestroy(); } @Override protected void onStopListening(Callback listener) { //暫停 }}

Callback的的介面有以下幾個

public class Callback {        private final IRecognitionListener mListener;        private Callback(IRecognitionListener listener) {            mListener = listener;        }                public void beginningOfSpeech() throws RemoteException {            if (DBG) Log.d(TAG, "beginningOfSpeech");            mListener.onBeginningOfSpeech();        }        public void bufferReceived(byte[] buffer) throws RemoteException {            mListener.onBufferReceived(buffer);        }                public void error(int error) throws RemoteException {            Message.obtain(mHandler, MSG_RESET).sendToTarget();            mListener.onError(error);        }               public void partialResults(Bundle partialResults) throws RemoteException {            mListener.onPartialResults(partialResults);        }               public void readyForSpeech(Bundle params) throws RemoteException {            mListener.onReadyForSpeech(params);        }              public void results(Bundle results) throws RemoteException {            Message.obtain(mHandler, MSG_RESET).sendToTarget();            mListener.onResults(results);        }                public void rmsChanged(float rmsdB) throws RemoteException {            mListener.onRmsChanged(rmsdB);        }    }

之後會有一篇文章將一個實現了百度語音辨識sdk的該服務代碼展示出來。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.