Implement Voice in Android Apps

Posted on Oct 13, 2013

Google Projects and Android provides an interesting feature of voice recognition. This feature can be easily implemented in applications and can be extended further in many directions to make some killer apps. The Android official documentation though provides a brief description about the classes involved in voice reorganization API, but don\'t explain them in much detail. There are no examples available as well. In this blog post, I\'ll discuss how to implement voice reorganization in your android apps.

Prior to implementing voice, I am assuming that you have all the basics clear about the development of android apps. Follow the procedure to create android application projects in eclipse or any other platform. If you are completely novice then you can start from here.

Once the blank project is created, follow these steps. First, Design the User Interface, create two xml files in layout folder, namely main.xml and voice.xml. Main.xml is the layout design for home activity and voice.xml is the layout design for the results page.

main.xml file
                    <?xml version="1.0" encoding="utf-8"?>                    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"                        android:orientation="vertical"                        android:layout_width="fill_parent"                         android:layout_height="fill_parent" >                    <TextView                         android:layout_width="fill_parent"                        android:layout_height="wrap_content"                        android:text="VoiceRecognition" />                    </LinearLayout>                    
voice.xml file
                    <?xml version="1.0" encoding="utf-8"?>                    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"                        android:layout_width="fill_parent"                        android:layout_height="fill_parent"                        android:orientation="vertical">                        <TextView                            android:layout_width="fill_parent"                            android:layout_height="wrap_content"                            android:paddingBottom="4dip"                            android:text="Click the button and start speaking" />                                             <Button android:id="@+id/Speak"                            android:layout_width="fill_parent"                            android:onClick="SpeakClicked"                            android:layout_height="wrap_content"                            android:text="Click Me!" />                                             <ListView android:id="@+id/list"                            android:layout_width="fill_parent"                            android:layout_height="0dip"                            android:layout_weight="1" />                                          </LinearLayout>                    

Next create a manifest file which will map the activities of the application to different actions. This is the configuration file for the activities. This file should be created in the root folder.

manifest.xml file
                        <?xml version="1.0" encoding="utf-8"?>                    <manifest xmlns:android="http://schemas.android.com/apk/res/android"                          package="com.shivam"                          android:versionCode="1"                          android:versionName="1.0">                        <application android:label="ImplementVoice" android:icon="@drawable/icon"                                android:debuggable="true">                            <activity android:name=".ImplementVoice"                                      android:label="@string/app_name">                                <intent-filter>                                    <action android:name="android.intent.action.MAIN" />                                    <category android:name="android.intent.category.LAUNCHER" />                                </intent-filter>                            </activity>                        </application>                    </manifest>                         

Now comes the main portion of the application, main.java file. Create a class called ImplementVoice and declare a variable named request_code in it. This variable is a checksum that is used to confirm the response when the user calls out to the voice recognition engine and it can take any value. Create an onCreate method which does the usual initialization on the start of the activity. This method is important as it also searches the android packageManager to check if there are any packages installed that can handle intents for ACTION_RECOGNIZE_SPEECH. This is done because to check we have a package installed that can do the translation, and if not we will disable the button.

Create another function named SpeakClicked and map it to the button, so this method is invoked when the button is clicked. This method starts VoiceRecognitionActivity intent which further invokes an activity that can handle the voice recognition. The onActivityResult is the callback function from the above chain calls, it first checks to see that the request code matches the one that was passed in, and ensures that the result is not erroneous. Next, the results are pulled out of the intent and set into the ListView to be displayed on the screen.

main.java file
                    // add the package name and the imports required (Eclipse will do that for you)                     public class ImplementVoice extends Activity                    {                        private static final int REQUEST_CODE = 1234;                        private ListView resultArray;                        //first function, called at the start of application                        @Override                        public void onCreate(Bundle savedInstanceState)                        {                            super.onCreate(savedInstanceState);                            setContentView(R.layout.voice);                            Button Speak = (Button) findViewById(R.id.Speak);                            resultArray = (ListView) findViewById(R.id.list);                            // Disable button if no recognition service is present, example in emulators                            PackageManager pm = getPackageManager();                            List activities = pm.queryIntentActivities(                                    new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);                            if (activities.size() == 0)                            {                                Speak.setEnabled(false);                                Speak.setText("Recognizer is not present in your device");                            }                        }                        // on buttonclick                        public void SpeakClicked(View v)                        {                            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, "Voice recognition");                            startActivityForResult(intent, REQUEST_CODE);                        }                        //Get the results and display on the scree in listview                        @Override                        protected void onActivityResult(int requestCode, int resultCode, Intent data)                        {                            if (requestCode == REQUEST_CODE && resultCode == RESULT_OK)                            {                                // Populate the resultArray with the String values the recognition engine thought it heard                                ArrayList matches = data.getStringArrayListExtra(                                        RecognizerIntent.EXTRA_RESULTS);                                resultArray.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1,                                        matches));                            }                            super.onActivityResult(requestCode, resultCode, data);                        }                    }                    
Feel free to discuss and share.This blogpost/tutorial is inspired from Here.