A previous article introduced the SpeechRecognizer class, which can be used as an external interface, and pass a componentname through intent to obtain a service that supports speech recognition, if some software for speech recognition is installed in a general user's phone, will have this ability, but if developers want to integrate their own speech recognition services through a company's SDK, they need to implement the Recognitionservice class.
This class is an abstract class that requires a developer to complete several of these abstract methods. Each method is described in the following code comment.
<pre name= "code" class= "Java" >public class Bdrecognitionservice extends Recognitionservice { @Override public void OnCreate () { super.oncreate (); Once the service is invoked @Override protected void onstartlistening (Intent recognizerintent, Callback mcallback) { //This method specifically implements the third-party SDK access,//recognizerintent carries the upper layer of the parameters//mcallback need to be the third-party SDK results callback to the interface, is the upper layer and the third-party SDK Bridge} @Override protected void OnCancel (Callback listener) {//logoff} @Override public void OnDestroy () {//Destroy Super.ondestroy ();} @Overrid e protected void onstoplistening (Callback listener) {//pause}}
The callback interface has the following several
public class Callback {private final irecognitionlistener mlistener; Private Callback (Irecognitionlistener listener) {Mlistener = listener; } public void Beginningofspeech () throws RemoteException {if (DBG) log.d (TAG, "BEGINNINGOFSPEEC H "); Mlistener.onbeginningofspeech (); } public void bufferreceived (byte[] buffer) throws RemoteException {mlistener.onbufferreceived (buffer); The public void error (int error) throws RemoteException {Message.obtain (Mhandler, Msg_res ET). Sendtotarget (); Mlistener.onerror (Error); } public void Partialresults (Bundle partialresults) throws RemoteException {MLISTENER.ONPARTIALR Esults (Partialresults); } public void Readyforspeech (Bundle params) throws remoteexception {Mlistener.onreadyforspeech (P Arams); } 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); } }
An article will then show the service code that implements the Baidu speech recognition SDK.