Thursday 24 October 2013

VOICE RECOGNITION IN ANDROID

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.The application may not work on the Android Emulator because it doesn’t support voice recognition. But the same can work on the phone.

Features:
               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.

So,lets start with the implementation of the Voice Recognization in android.

First create project by Eclipse > File> New Project>Android Application Project. Fill the required field, i.e Application Name, Project Name and Package. Now press the next button. 
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 has embedded comments which makes it easy to understand.

VoiceRecognitionActivity.java
 import java.util.ArrayList;  
 import java.util.List;  
 import android.app.Activity;  
 import android.app.SearchManager;  
 import android.content.Intent;  
 import android.content.pm.PackageManager;  
 import android.content.pm.ResolveInfo;  
 import android.os.Bundle;  
 import android.speech.RecognizerIntent;  
 import android.view.View;  
 import android.widget.AdapterView;  
 import android.widget.ArrayAdapter;  
 import android.widget.Button;  
 import android.widget.EditText;  
 import android.widget.ListView;  
 import android.widget.Spinner;  
 import android.widget.Toast;  
 public class VoiceRecognitionActivity extends Activity {  
  private static final int VOICE_RECOGNITION_REQUEST_CODE = 1001;  
  private EditText m_etTextHint;  
  private ListView m_lvTextMatches;  
  private Spinner m_spTextMatches;  
  private Button m_btnSpeak;  
  @Override  
  public void onCreate(Bundle savedInstanceState) {  
  super.onCreate(savedInstanceState);  
  setContentView(R.layout.activity_voice_recognition);  
  m_etTextHint = (EditText) findViewById(R.id.vr_etTextHint);  
  m_lvTextMatches = (ListView) findViewById(R.id.vr_lvTextMatches);  
  m_spTextMatches = (Spinner) findViewById(R.id.vr_spNoOfMatches);  
  m_btnSpeak = (Button) findViewById(R.id.vr_btnSpeak);  
  }  
  public void checkVoiceRecognition() {  
  // Check if voice recognition is present  
  PackageManager m_pmanger = getPackageManager();  
  List<ResolveInfo> m_activities = m_pmanger.queryIntentActivities(new Intent(  
   RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);  
  if (m_activities.size() == 0) {  
   m_btnSpeak.setEnabled(false);  
   Toast.makeText(this, "Voice recognizer not present",  
    Toast.LENGTH_SHORT).show();  
  }  
  }  
  public void speak(View p_view) {  
  Intent m_intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);  
  // Specify the calling package to identify your application  
  m_intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass()  
   .getPackage().getName());  
  // Display an hint to the user about what he should say.  
  m_intent.putExtra(RecognizerIntent.EXTRA_PROMPT, m_etTextHint.getText()  
   .toString());  
  // Given an hint to the recognizer about what the user is going to say  
  m_intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,  
   RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);  
  // If number of Matches is not selected then return show toast message  
  if (m_spTextMatches.getSelectedItemPosition() == AdapterView.INVALID_POSITION) {  
   Toast.makeText(this, "Please select No. of Matches from spinner",  
    Toast.LENGTH_SHORT).show();  
   return;  
  }  
  int m_noOfMatches = Integer.parseInt(m_spTextMatches.getSelectedItem()  
   .toString());  
  // Specify how many results you want to receive. The results will be  
  // sorted where the first result is the one with higher confidence.  
  m_intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, m_noOfMatches);  
  startActivityForResult(m_intent, VOICE_RECOGNITION_REQUEST_CODE);  
  }  
  @Override  
  protected void onActivityResult(int p_requestCode, int p_resultCode, Intent p_data) {  
 if (p_requestCode == VOICE_RECOGNITION_REQUEST_CODE)  
 //If Voice recognition is successful then it returns RESULT_OK  
  if(p_resultCode == RESULT_OK) {  
  ArrayList<String> m_textMatchList = p_data  
  .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);  
   if (!m_textMatchList.isEmpty()) {  
  // If first Match contains the 'search' word  
  // Then start web search.  
  if (m_textMatchList.get(0).contains("search")) {  
  String m_searchQuery = m_textMatchList.get(0).replace("search"," ");  
  Intent m_searchIntent = new Intent(Intent.ACTION_WEB_SEARCH);  
  m_searchIntent.putExtra(SearchManager.QUERY, m_searchQuery);  
       startActivity(m_searchIntent);  
   } else {  
  // populate the Matches  
  m_lvTextMatches.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,  
   m_textMatchList));  
    }  
   }  
   //Result code for various error.   
   }else if(p_resultCode == RecognizerIntent.RESULT_AUDIO_ERROR){  
   showToastMessage("Audio Error");  
   }else if(p_resultCode == RecognizerIntent.RESULT_CLIENT_ERROR){  
   showToastMessage("Client Error");  
   }else if(p_resultCode == RecognizerIntent.RESULT_NETWORK_ERROR){  
   showToastMessage("Network Error");  
   }else if(p_resultCode == RecognizerIntent.RESULT_NO_MATCH){  
   showToastMessage("No Match");  
   }else if(p_resultCode == RecognizerIntent.RESULT_SERVER_ERROR){  
   showToastMessage("Server Error");  
   }  
  super.onActivityResult(p_requestCode, p_resultCode, p_data);  
  }  
  void showToastMessage(String p_message){  
  Toast.makeText(this, p_message, Toast.LENGTH_SHORT).show();  
  }  
 }  

activity_voice_recognition.xml
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:tools="http://schemas.android.com/tools"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   android:orientation="vertical" >  
   <EditText  
     android:id="@+id/vr_etTextHint"  
     android:gravity="top"  
     android:inputType="textMultiLine"  
     android:lines="1"  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"   
     android:hint="@string/lbl_SearchHint"/>  
   <Button  
     android:id="@+id/vr_btnSpeak"  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:onClick="speak"  
     android:padding="@dimen/padding_medium"  
     android:text="@string/lbl_btnSpeak"  
     tools:context=".VoiceRecognitionActivity" />  
   <Spinner  
     android:id="@+id/vr_spNoOfMatches"  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:entries="@array/saNoOfMatches"  
     android:prompt="@string/lbl_sNoOfMatches"/>  
   <TextView  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:text="@string/lbl_tvTextMatches"  
     android:textStyle="bold" />  
   <ListView  
     android:id="@+id/vr_lvTextMatches"  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content" />  
 </LinearLayout>  

Now add the string constants in string.xml. This file should look similar to the one shown below.

string.xml
 <resources>  
   <string name="app_name">VoiceRecognitionExample</string>  
   <string name="btSpeak">Speak</string>  
   <string name="menu_settings">Settings</string>  
   <string name="title_activity_voice_recognition">Voice Recognition</string>  
   <string name="tvTextMatches">Text Matches</string>  
   <string name="sNoOfMatches">No of Matches</string>  
   <string name="etSearchHint">Speech hint here</string>  
   <string-array name="saNoOfMatches">  
     <item>1</item>  
     <item>2</item>  
     <item>3</item>  
     <item>4</item>  
     <item>5</item>  
     <item>6</item>  
     <item>7</item>  
     <item>8</item>  
     <item>9</item>  
     <item>10</item>  
   </string-array>  
 </resources>  

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.
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
   package="com.rakesh.voicerecognitionexample"  
   android:versionCode="1"  
   android:versionName="1.0" >  
   <uses-sdk  
     android:minSdkVersion="8"  
     android:targetSdkVersion="15" />  
   <!-- Permissions -->  
  <uses-permission android:name="android.permission.INTERNET" />  
   <application  
     android:icon="@drawable/ic_launcher"  
     android:label="@string/app_name"  
     android:theme="@style/AppTheme" >  
     <activity  
       android:name=".VoiceRecognitionActivity"  
       android:label="@string/title_activity_voice_recognition" >  
       <intent-filter>  
       <action android:name="android.intent.action.MAIN" />  
    <category android:name="android.intent.category.LAUNCHER" />  
       </intent-filter>  
     </activity>  
   </application>  
 </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 screens on your device screen.

No comments:

Post a Comment