Wednesday, September 8, 2010

android voice recognition

If you are willing to develop a voice recognition system in the android phone, you will follow these steps:

create an activity.
create a button which launch the voice activity.
first you will get the package manager to find that the device has the ability to perform voice recognition.
PackageManager pm = getPackageManager();
Get the list from the package manager as:
List activities = pm.queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
check its size
activities.size() if it is greater than the zero then go for further processing.

Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo");
        startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);

this will launch the voice recognition activity and return a result.
Override this method

@Override
    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 matches = data.getStringArrayListExtra(
                    RecognizerIntent.EXTRA_RESULTS);
            mList.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1,
                    matches));
        }

        super.onActivityResult(requestCode, resultCode, data);
    }


Full code of the application can be:

public class Main extends Activity implements OnClickListener{
   
    private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;
   
    private ListView mList;
   
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        Button speakButton = (Button) findViewById(R.id.btn_speak);
        mList = (ListView) findViewById(android.R.id.list);
       
        PackageManager pm = getPackageManager();
        List activities = pm.queryIntentActivities(
                new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
        if (activities.size() != 0) {
            speakButton.setOnClickListener(this);
        } else {
            speakButton.setEnabled(false);
            speakButton.setText("Recognizer not present");
        }
    }
   
    public void onClick(View v) {
        if (v.getId() == R.id.btn_speak) {
            startVoiceRecognitionActivity();
        }
    }
   
  
    private void startVoiceRecognitionActivity() {
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo");
        startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
    }
   
    @Override
    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 matches = data.getStringArrayListExtra(
                    RecognizerIntent.EXTRA_RESULTS);
            mList.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1,
                    matches));
        }

        super.onActivityResult(requestCode, resultCode, data);
    }
}


Please comment if you have any problem in this code.

Tuesday, September 7, 2010

Event Handling in Android

There are many events in android which can be handle through code but I am currently go with onClickListener.
As you know that all events are listen through a listener and we will work with OnClickListener.
You have a button in xml layout and you are accessing it with the code:
       Button myBtn = (Button) findViewById(R.id.my_button);
Now we are register a listener with it as
        myBtn.setOnClickListener(new MyListener ());//Object_of_OnClickListener_class

Now I am going to develop a class which will be implement OnClickListener which is member of android.view.View.OnClickListener.

Import android.view.View.OnClickListener;

public class MyListener implements OnClickListener{
   public void onClick(View v) {
         //your action
   }
}

Now write your logic for your action.

Practice it and let me know if you have any problem.