Thursday, May 17, 2012

Speech Input API in Android

Google's servers currently support English, Mandarin Chinese, and Japanese.There are basically to two models supported now: "free_form" for dictation, or "web_search" for shorter,
 search-like phrases. The web search model is available in all three languages, while free-form has primarily been optimized for English.

*How to use it in Application:*

You can use it by implementing simple code described simple steps below:

*A:)First approach :Simply start RecognizerIntent*

1:)You have to check whether Google's Voice Search application is installed or not:
        // Check to see if a recognition activity is present
        PackageManager pm = getPackageManager();
        List<ResolveInfo> activities = pm.queryIntentActivities(
                new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
        if (activities.size() != 0) {
            speakButton.setOnClickListener(this);//simple button which is enabled if Google's Voice Search application is installed
        } else {
          //if Google's Voice Search application is not installed,it will not work.
            speakButton.setText("Recognizer not present");

2:)Than you have to call RecognizerIntent to start the speech recognition activity.

Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);//set model for search
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo");//text displayed when starting intent
        startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);


3;)After completing ,you will get results in startActivitForResult method

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
            // Fill the list view with the strings the recognizer thought it could have heard
            ArrayList<String> matches = data.getStringArrayListExtra(
                    RecognizerIntent.EXTRA_RESULTS);//this will get you result texts of speech converted
            mList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,

        super.onActivityResult(requestCode, resultCode, data);


*B:)Implementing SpeechRecognizer:*
Described above is simple demo and easiest way to integrate speech api into your app.It is also included in api demo.Problem with this method is there was too little control over the speech recognition error handling. Second approach is the  using the SpeechRecognizer directly in  code.  With this advantage is our application is not being paused and resumed in order to get the results from the speech recognition activity.

1;)You have to implement described below code in onCreate() method :

        mSpeechRecognizer = SpeechRecognizer
        mRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);//model for search
        mRecognizerIntent.putExtra("calling_package", "com.indianic.SpeechTest");//adding your package name

2:)Than you have to implement listener for speech recognizer as described below in which you can handle various events for SpeechRecognizer:

private RecognitionListener mRecognitionListener = new RecognitionListener() {
        public void onBufferReceived(byte[] buffer) {


        public void onError(int error) {


        public void onEvent(int eventType, Bundle params) {


        public void onPartialResults(Bundle partialResults) {
            Log.d(TAG, "onPartialResults");

        public void onReadyForSpeech(Bundle params) {

        public void onResults(Bundle results) {


        public void onBeginningOfSpeech() {


        public void onEndOfSpeech() {



3:)Than you have to just start speechrecognizer for listening in onResume() method:
For using above method,You have to add permission in manifest fro recording audio:
<uses-permission android:name="android.permission.RECORD_AUDIO"/>

No comments:

Post a Comment