Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding Voice Recognition Service

I am trying to understand the functioning of RecognitionService and RecognitionService.Callback. I am pretty new to this framework and would like to know how to call the onStartListening() function in RecognitionService. I saw the post How to register a custom speech recognition service? but I had inserted log messages in all the main functions to see which one is being called when.

I have also looked at the sample app in the sdk, but it does a pretty bad job of explaining how things happen. I want to call the startService from an activity.

I use the following intent

Intent startServiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    startServiceIntent.setClass(this, SimpleVoiceService.class);

    startService(startServiceIntent);

Could someone please help me in getting this working. It will be great if someone could point me to a tutorial on this, or a describe the general flow of how to do it.

Thanks a lot.

like image 511
nehiljain Avatar asked Nov 13 '22 01:11

nehiljain


1 Answers

The basic idea is to use SpeechRecognizer to connect to the RecognitionService that the user has chosen in the general Android settings.

SpeechRecognizer sr = SpeechRecognizer.createSpeechRecognizer(context);
sr.setRecognitionListener(new RecognitionListener() {
    @Override
    public void onResults(Bundle b) { /* ... */ }

    // other required methods
});

Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "");
sr.startListening(intent);

You must provide the implementation of the RecognitionListener-methods, allowing you update the UI in response to the speech recognition events (user started speaking, partial results are available, user stopped speaking, transcribing is still going on, an error occurred, etc.).

See the full implementation in the source code of some keyboard apps, e.g. the VoiceInput class in Hacker's Keyboard.

like image 63
Kaarel Avatar answered Dec 28 '22 07:12

Kaarel