Android has introduced support for speech recognition. Speech recognition is mainly located in the Android.speech package, the main classes include SpeechRecognizer, Recognitionservice, Recognizerintent, Recognizerresultsintent, Classes and interfaces such as Recognitionlistener.
SpeechRecognizer provides an interface to access speech recognition services, the following are common methods of SpeechRecognizer :
<span style= "FONT-SIZE:18PX;" > Createspeechrecognizer ()/ /Create a speech recognizer Setrecognitionlistener () //Set Listener startlistening () //Start speech recognition stoplistening () //End speech recognition </span>
Speech recognition requires the "Android.permission.RECORD_AUDIO" permission.
In order to make speech recognition, it is necessary to publish a speech recognition activity, then the speech recognition engine will be started, and the user's voice will be processed, the result of processing is returned through the Onactivityresult () method. Here's an implementation in Android:
SpeechRecognizer sr = Speechrecognizer.createspeechrecognizer (this, new ComponentName (Getpackagename (), BDRecognitionService.class.getName ())); Sr.setrecognitionlistener (New Listener ()); Sr.startlistening (New Intent (Getintent (). Getaction ()). Putextras (Getintent ()));
callback method
<span style= "TEXT-INDENT:24PX; BACKGROUND-COLOR:RGB (240, 240, 240); " >class Listener implements recognitionlistener{</span> @Override public void Onreadyforspeech (Bundl e params) {} @Override public void Onbeginningofspeech () {} @ Override public void onrmschanged (float rmsdb) {} @Override public void Onbufferr Eceived (byte[] buffer) {} @Override public void Onendofspeech () {} @Override public void OnError (int. error) {} @Override public void on Results (Bundle Results) {//specific process recognition result} @Override public void Onpartialresults ( Bundle partialresults) {} @Override public void onEvent (int eventtype, bundle params) {}}
Note: Although Android provides this interface, it does not provide specific recognition capabilities
New ComponentName (Getpackagename (),BDRecognitionService.class.getName ())
As a result, the parameters at the time of instantiation specify the service that implements the function, which requires the developer to implement the Recognitionservice interface, and the next article will describe how to implement the class.