Tutorial details

Android Voice Recognition Tutorial | App Code for Sale | Preview

Android Voice Recognition Tutorial | Android Tutorial

How to work with Voice Recognition in Android

Overview PAGE TOP

You may have heard about the “ Google Now project” where you give the voice command and Android fetches result for you. It recognizes your voice and converts it into the text or takes the appropriate action. Have you ever thought how is it done? If your answer is voice recognition API, then you are absolutly right. Recently while playing with Android voice recognition APIs, I found some interesting stuffs. APIs are really easy to use with application. Given below is a small tutorial on voice/speech recognition API. The final application will look similar to that of application shown below. The application may not work on the Android Emulator because it doesn’t support voice recognition. But the same can work on the phone.

voice-recognition-tutorial-example-2.png

Project Information: Meta-data about the project.

Platform Version : Android API Level 15.

IDE : Eclipse Helios Service Release 2

Emulator : Android 4.1(API 16)

Prerequisite: Preliminary knowledge of Android application framework, and Intent.

Voice recognition feature can be achieved by RecognizerIntent. Create an Intent of type RecognizerIntent and pass the extra parameters and start activity for the result. It basically starts the recognizer prompt customized by your extra parameters. Internally voice recognition communicates with the server and gets the results. So you must provide the internet access permission for the application. Android Jelly Bean(API level 16) doesn’t require internet connection to perform voice recognition. Once the voice recognition is done, recognizer returns value in onActivityResult() method parameters.

First create project by Eclipse > File> New Project>Android Application Project. The following dialog box will appear. Fill the required field, i.e Application Name, Project Name and Package. Now press the next button.

Android-project-voice-recognition-3.png

Once the dialog box appears, select the BlankActivity and click the next button.

Android-project-voice-recognition-2_ta.png

Fill the Activity Name and Layout file name in the dialog box shown below and hit the finish button.

Android-project-voice-recognition-3_ki.png

This process will setup the basic project files. Now we are going to add four buttons in the activityvoicerecognition.xml file. You can modify the layout file using either Graphical Layout editor or xml editor. The content of the file is shown below. As you may notice that we have attached speak() method with button in onClick tag. When the button gets clicked, the speak() method will get executed. We will define speak() in the main activity.

01  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
02      xmlns:tools="http://schemas.android.com/tools"
03      android:layout_width="match_parent"
04      android:layout_height="match_parent"
05      android:orientation="vertical" >
06  
07      <EditText
08          android:id="@+id/etTextHint"
09          android:gravity="top"
10          android:inputType="textMultiLine"
11          android:lines="1"
12          android:layout_width="match_parent"
13          android:layout_height="wrap_content"
14          android:text="@string/etSearchHint"/>
15  
16      <Button
17          android:id="@+id/btSpeak"
18          android:layout_width="match_parent"
19          android:layout_height="wrap_content"
20          android:onClick="speak"
21          android:padding="@dimen/padding_medium"
22          android:text="@string/btSpeak"
23          tools:context=".VoiceRecognitionActivity" />
24  
25      <Spinner
26          android:id="@+id/sNoOfMatches"
27          android:layout_width="match_parent"
28          android:layout_height="wrap_content"
29          android:entries="@array/saNoOfMatches"
30          android:prompt="@string/sNoOfMatches"/>
31  
32      <TextView
33          android:layout_width="match_parent"
34          android:layout_height="wrap_content"
35          android:text="@string/tvTextMatches"
36          android:textStyle="bold" />
37  
38      <ListView
39          android:id="@+id/lvTextMatches"
40          android:layout_width="match_parent"
41          android:layout_height="wrap_content" />
42  
43  </LinearLayout>

You may have noticed that the String constants are being accessed from the resource. Now add the string constants in string.xml. This file should look similar to the one shown below.

01  <resources>
02      <string name="app_name">VoiceRecognitionExample</string>
03      <string name="btSpeak">Speak</string>
04      <string name="menu_settings">Settings</string>
05      <string name="title_activity_voice_recognition">Voice Recognition</string>
06      <string name="tvTextMatches">Text Matches</string>
07      <string name="sNoOfMatches">No of Matches</string>
08      <string name="etSearchHint">Speech hint here</string>
09      <string-array name="saNoOfMatches">
10          <item>1</item>
11          <item>2</item>
12          <item>3</item>
13          <item>4</item>
14          <item>5</item>
15          <item>6</item>
16          <item>7</item>
17          <item>8</item>
18          <item>9</item>
19          <item>10</item>
20      </string-array>
21  </resources>

Now let’s define the Activity class. This activity class, with the help of checkVoiceRecognition() method, will first check whether the Voice recognition is available or not. If voice recognition feature is not available, then toast a message and disable the button. Speak() method is defined here which gets called once the speak button is pressed. In this method we are creating RecognizerIntent and passing the extra parameters. The code below has embedded comments which makes it easy to understand.

001 package com.rakesh.voicerecognitionexample;
002 
003 import java.util.ArrayList;
004 import java.util.List;
005 
006 import android.app.Activity;
007 import android.app.SearchManager;
008 import android.content.Intent;
009 import android.content.pm.PackageManager;
010 import android.content.pm.ResolveInfo;
011 import android.os.Bundle;
012 import android.speech.RecognizerIntent;
013 import android.view.View;
014 import android.widget.AdapterView;
015 import android.widget.ArrayAdapter;
016 import android.widget.Button;
017 import android.widget.EditText;
018 import android.widget.ListView;
019 import android.widget.Spinner;
020 import android.widget.Toast;
021 
022 public class VoiceRecognitionActivity extends Activity {
023  private static final int VOICE_RECOGNITION_REQUEST_CODE = 1001;
024 
025  private EditText metTextHint;
026  private ListView mlvTextMatches;
027  private Spinner msTextMatches;
028  private Button mbtSpeak;
029 
030  @Override
031  public void onCreate(Bundle savedInstanceState) {
032   super.onCreate(savedInstanceState);
033   setContentView(R.layout.activity_voice_recognition);
034   metTextHint = (EditText) findViewById(R.id.etTextHint);
035   mlvTextMatches = (ListView) findViewById(R.id.lvTextMatches);
036   msTextMatches = (Spinner) findViewById(R.id.sNoOfMatches);
037   mbtSpeak = (Button) findViewById(R.id.btSpeak);
038   checkVoiceRecognition()
039  }
040 
041  public void checkVoiceRecognition() {
042   // Check if voice recognition is present
043   PackageManager pm = getPackageManager();
044   List<resolveinfo> activities = pm.queryIntentActivities(new Intent(
045     RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
046   if (activities.size() == 0) {
047    mbtSpeak.setEnabled(false);
048    mbtSpeak.setText("Voice recognizer not present")
049    Toast.makeText(this, "Voice recognizer not present",
050      Toast.LENGTH_SHORT).show();
051   }
052  }
053 
054  public void speak(View view) {
055   Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
056 
057   // Specify the calling package to identify your application
058   intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass()
059     .getPackage().getName());
060 
061   // Display an hint to the user about what he should say.
062   intent.putExtra(RecognizerIntent.EXTRA_PROMPT, metTextHint.getText()
063     .toString());
064 
065   // Given an hint to the recognizer about what the user is going to say
066   //There are two form of language model available
067   //1.LANGUAGE_MODEL_WEB_SEARCH : For short phrases
068   //2.LANGUAGE_MODEL_FREE_FORM  : If not sure about the words or phrases and its domain.
069 intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
070     RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
071 
072   // If number of Matches is not selected then return show toast message
073   if (msTextMatches.getSelectedItemPosition() == AdapterView.INVALID_POSITION) {
074    Toast.makeText(this, "Please select No. of Matches from spinner",
075      Toast.LENGTH_SHORT).show();
076    return;
077   }
078 
079   int noOfMatches = Integer.parseInt(msTextMatches.getSelectedItem()
080     .toString());
081   // Specify how many results you want to receive. The results will be
082   // sorted where the first result is the one with higher confidence.
083   intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, noOfMatches);
084   //Start the Voice recognizer activity for the result.
085   startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
086  }
087 
088  @Override
089  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
090   if (requestCode == VOICE_RECOGNITION_REQUEST_CODE)
091 
092    //If Voice recognition is successful then it returns RESULT_OK
093    if(resultCode == RESULT_OK) {
094 
095     ArrayList<string> textMatchList = data
096     .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
097 
098     if (!textMatchList.isEmpty()) {
099      // If first Match contains the 'search' word
100      // Then start web search.
101      if (textMatchList.get(0).contains("search")) {
102 
103         String searchQuery = textMatchList.get(0);
104                                            searchQuery = searchQuery.replace("search","");
105         Intent search = new Intent(Intent.ACTION_WEB_SEARCH);
106         search.putExtra(SearchManager.QUERY, searchQuery);
107         startActivity(search);
108      } else {
109          // populate the Matches
110          mlvTextMatches
111       .setAdapter(new ArrayAdapter<string>(this,
112         android.R.layout.simple_list_item_1,
113         textMatchList));
114      }
115 
116     }
117    //Result code for various error.
118    }else if(resultCode == RecognizerIntent.RESULT_AUDIO_ERROR){
119     showToastMessage("Audio Error");
120    }else if(resultCode == RecognizerIntent.RESULT_CLIENT_ERROR){
121     showToastMessage("Client Error");
122    }else if(resultCode == RecognizerIntent.RESULT_NETWORK_ERROR){
123     showToastMessage("Network Error");
124    }else if(resultCode == RecognizerIntent.RESULT_NO_MATCH){
125     showToastMessage("No Match");
126    }else if(resultCode == RecognizerIntent.RESULT_SERVER_ERROR){
127     showToastMessage("Server Error");
128    }
129   super.onActivityResult(requestCode, resultCode, data);
130  }
131  /**
132  * Helper method to show the toast message
133  **/
134  void showToastMessage(String message){
135   Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
136  }
137 }

Here is the Android manifest file. You can see that INTERNET permission has been provided to the application because of the voice recognizer’s need to send the query to the server and get the result.

01  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
02      package="com.rakesh.voicerecognitionexample"
03      android:versionCode="1"
04      android:versionName="1.0" >
05  
06      <uses-sdk
07          android:minSdkVersion="8"
08          android:targetSdkVersion="15" />
09      <!-- Permissions -->
10   <uses-permission android:name="android.permission.INTERNET" />
11  
12      <application
13          android:icon="@drawable/ic_launcher"
14          android:label="@string/app_name"
15          android:theme="@style/AppTheme" >
16  
17          <activity
18              android:name=".VoiceRecognitionActivity"
19              android:label="@string/title_activity_voice_recognition" >
20              <intent-filter>
21                  <action android:name="android.intent.action.MAIN" />
22  
23                  <category android:name="android.intent.category.LAUNCHER" />
24              </intent-filter>
25          </activity>
26      </application>
27  </manifest>

Once you are done with coding then connect the phone with your system and hit the run button on Eclipse IDE. Eclipse will install and launch the application. You will see the following activities on your device screen.

trew.JPG

In the next tutorial, we will learn how to use the new voice recognition API introduced in Android Jelly Bean(API level 16) along with the examples.

If you are interested in the source code, then you can get it from github.

Reference PAGE TOP

http://www.javacodegeeks.com/2012/08/android-voice-recognition-tutorial.html

0 Comments Leave a comment

Please login in order to leave a comment.

Newest first
!

Sign-in to your Chupamobile Account.

The Easiest way to Launch your next App or Game.

Join Chupamobile and get instant access to thousands of ready made App and Game Templates.

Creating an account means you’re okay with Chupamobile’s Terms of Service and Privacy Policy.