当前位置: 代码迷 >> Android >> 实现Android语音识别服务接口 RecognitionService的步骤
  详细解决方案

实现Android语音识别服务接口 RecognitionService的步骤

热度:49   发布时间:2016-04-28 05:08:08.0
实现Android语音识别服务接口 RecognitionService的方法

之前的一篇文章介绍过SpeechRecognizer类,该类可以作为对外的一个接口,并通过Intent传递一个ComponentName获取可支持语音识别的功能的服务,一般的用户手机中如果安装了语音识别的一些软件,就会拥有这样的能力,但是如果开发者希望自己通过某公司的sdk集成自己的语音识别服务,那么就需要实现RecognitionService这个类。

这个类是个抽象类,需要开发者完成其中的几个抽象方法。下面的代码注释中对每个方法进行了说明。

<pre name="code" class="java">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的该服务代码展示出来。


  相关解决方案