Saturday 1 February 2014

Voice/web search and speech recognition

Speech Recognition:

 In this world of smartphones, you can write text message, can search content on internet without writing anything, Speech Recognition will do that for you, you just need to say it.
 Speech recognition is the translation of spoken words into text.So Speak instead of type, using your voice to control your phone or tablet. Search the web and your phone, call your contacts, send emails, get directions and listen to music, all with speech Recognition. And Here is the basic post on how to listen voice and search the spoken text on web.  

On Launch

voice search

OnSpeak

voice search

voice searchvoice search
When Spoke Search Hamad shaikh:

voice searchvoice search

Create new Android Project
Project Name: VoiceRecognition//tested from 2.3.3 to current android sdk 
Build Target: Android 2.3.3   //or greater than that
Application Name: Voice Recognition
Package Name: com.shaikhhamadali.blogspot.voicerecognition
Create layout file: layout_voice_recognition


 1. create layout:


<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/eTTextHint"
        android:gravity="top"
        android:inputType="textMultiLine"
        android:lines="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/etSearchHint"/>

    <Button
        android:id="@+id/btnSpeak"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="speak"
        android:padding="10dp"
        android:text="@string/btSpeak"
        tools:context=".VoiceRecognitionActivity" />

    <Spinner
        android:id="@+id/spTextMatches"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/saNoOfMatches"
        android:prompt="@string/sNoOfMatches"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/tvTextMatches"
        android:textStyle="bold" />

    <ListView
        android:id="@+id/lVTextMatches"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

-Manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.shaikhhamadali.blogspot.voicerecognition"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="17" />
    
    <!-- Permissions -->
    <uses-permission android:name="android.permission.INTERNET" />
    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.shaikhhamadali.blogspot.voicerecognition.VoiceRecognitionActivity"
            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>


-Strings file:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">VoiceRecognition</string>
    <string name="action_settings">Settings</string>
    <string name="btSpeak">Speak</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>


2.code of activity:


package com.shaikhhamadali.blogspot.voicerecognition;

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 {
 //variables
 //code to get result
 private static final int VOICE_RECOGNITION_REQUEST_CODE = 1001;
 //declare controls
 private EditText eTTextHint;
 private ListView lVTextMatches;
 private Spinner spTextMatches;
 private Button btnSpeak;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.layout_voice_recognition);
  //Initialize controls
  initializeControls();
  // Check if voice recognition is present 
  isVoiceRecognitionAvaialble();
 }
 private void initializeControls() {
  eTTextHint = (EditText) findViewById(R.id.eTTextHint);
  lVTextMatches = (ListView) findViewById(R.id.lVTextMatches);
  spTextMatches = (Spinner) findViewById(R.id.spTextMatches);
  btnSpeak = (Button) findViewById(R.id.btnSpeak);
 }
 public void isVoiceRecognitionAvaialble() {
  PackageManager pm = getPackageManager();
  //start an activity that will prompt the user for speech and send it through a speech recognizer
  List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(
    RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
  //check's that is activity available
  if (activities.size() == 0) {
   btnSpeak.setEnabled(false);
   btnSpeak.setText("Voice recognizer not present");
   Toast.makeText(this, "Voice recognizer not present",
     Toast.LENGTH_SHORT).show();
  }
 }

 public void speak(View view) {
  //create instance of Intent and pass RecognizerIntent.ACTION_RECOGNIZE_SPEECH
  Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

  // Specify the calling package to identify your application
  intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass()
    .getPackage().getName());

  // Display a hint to the user about what he should say.
  intent.putExtra(RecognizerIntent.EXTRA_PROMPT, eTTextHint.getText()
    .toString());

  // Given an hint to the recognizer about what the user is going to say
  //There are two form of language model available
  //1.LANGUAGE_MODEL_WEB_SEARCH : For short phrases
  //2.LANGUAGE_MODEL_FREE_FORM  : If not sure about the words or phrases and its domain.
  intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
    RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);

  // If number of Matches is not selected then return show toast message
  if (spTextMatches.getSelectedItemPosition() == AdapterView.INVALID_POSITION) {
   Toast.makeText(this, "Please select No. of Matches from spinner",
     Toast.LENGTH_SHORT).show();
   return;
  }

  int noOfMatches = Integer.parseInt(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.
  intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, noOfMatches);
  //Start the Voice recognizer activity for the result.
  startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
 }

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (requestCode == VOICE_RECOGNITION_REQUEST_CODE)

   //If Voice recognition is successful then it returns RESULT_OK
   if(resultCode == RESULT_OK) {
    // creates list of all the text matches
    ArrayList<String> textMatchList = data
      .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
    //proceed further if is list is not empty
    if (!textMatchList.isEmpty()) {
     // If first Match contains the 'search' word
     // Then start web search.
     if (textMatchList.get(0).contains("search")) {

      String searchQuery = textMatchList.get(0);
      searchQuery = searchQuery.replace("search","");
      //create instance of Intent and pass Intent.ACTION_WEB_SEARCH
      Intent search = new Intent(Intent.ACTION_WEB_SEARCH);
      //put text you want to search
      search.putExtra(SearchManager.QUERY, searchQuery);
      //start activity and pass search intent
      startActivity(search);
     } else {
      // populate the Matches
      lVTextMatches.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,textMatchList));
     }

    }
    //Result code for various error.
   }else if(resultCode == RecognizerIntent.RESULT_AUDIO_ERROR){
    showToastMessage("Audio Error");
   }else if(resultCode == RecognizerIntent.RESULT_CLIENT_ERROR){
    showToastMessage("Client Error");
   }else if(resultCode == RecognizerIntent.RESULT_NETWORK_ERROR){
    showToastMessage("Network Error");
   }else if(resultCode == RecognizerIntent.RESULT_NO_MATCH){
    showToastMessage("No Match");
   }else if(resultCode == RecognizerIntent.RESULT_SERVER_ERROR){
    showToastMessage("Server Error");
   }
  super.onActivityResult(requestCode, resultCode, data);
 }
 /**
  * Helper method to show the toast message
  **/
 void showToastMessage(String message){
  Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
 }
}

3. note that:


      4. conclusion:

      • Some information about how to use Voice Recognition Service in Android using RecognizerIntent.
      • know how to use Intents to Recognize voice, WebSearch and what search keyword do.

      5. About the post:

      • The code seems to explain itself due to comments, but if you have any questions you can freely ask too!
      • Don’t mind to write a comment whatever you like to ask, to know,to suggest or recommend.
      • Hope you enjoy it!

      6. Source Code:

              you can download the source code GoogleDriveGithub