Friday, 19 July 2013

Voice Recognition(Speech to text) in Android


Hello Guys, Hope You all are doing well. Today  I am going to show an example on Voice recognition in Android. My Previous Post was on Text To speech . Now I am going to show  Speech to text and search it on Google.

Android natively provides feature of Speech to Text.Voice recognition featured in android is achieved using the RecognizerIntent.Use the Recognizer class in an intent to call the voice API.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.

Here we check for the recognizer in the device if available the speech to text happen else we show a toast displaying 'Recognizer Not Found'

Note: This application will not work in emulator since it does not have the recognizer, so test it in a device and it also requires an internet connection.



                 

Above given image is final  output of voice recognition working sample app. Just create a Android project named it VoiceRecognition in your Eclipse. Copy and paste following source code in your class. 
Java Class
1. VoiceActivity.java
Xml file
1. activity_voice.xml
Now put following code in your 
Voice Activity. java. 
package com.sks.texttospeech;
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.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class VoiceActivity extends Activity{
private static final int REQUEST_CODE = 1234;
private ListView matchList;
Button speak_button;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_voice_recignition);
speak_button = (Button) findViewById(R.id.speakButton);
matchList = (ListView) findViewById(R.id.list);
// Disable button if no recognition service is present
PackageManager pm = getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(
new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
if (activities.size() == 0)
{
speak_button.setEnabled(false);
Toast.makeText(getApplicationContext(), "Recognizer Not Found", 1000).show();
}
speak_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startVoiceRecognitionActivity();
}
});
matchList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
String item = matchList.getAdapter().getItem(position).toString();
Toast.makeText(VoiceActivity.this, item + " selected", Toast.LENGTH_LONG).show();
Intent search = new Intent(Intent.ACTION_WEB_SEARCH);
search.putExtra(SearchManager.QUERY, item);
startActivity(search);
}
});
}
private void startVoiceRecognitionActivity()
{
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
//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_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "saurabhsharma Voice Recognition...");
startActivityForResult(intent, REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK)
{
ArrayList<String> matches = data.getStringArrayListExtra(
RecognizerIntent.EXTRA_RESULTS);
matchList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,matches));
}
super.onActivityResult(requestCode, resultCode, data);
}
}

Now Put following code in your 
activity_voice.xml
<?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:layout_margin="10dp"
android:gravity="center_horizontal"
android:textStyle="bold"
android:textColor="#A52A2A"
android:text="Click microPhone and start speaking" />
<Button
android:id="@+id/speakButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@drawable/voic_img1" />
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" />
</LinearLayout>

Finally put internet permission in your manifest file.
<uses-permission android:name="android.permission.INTERNET" />
  Run this code on Actual Device and  Enjoy the Speech Api Of android.
                                       Happy Coding !!!

5 comments:

  1. Hi! What do you mean when you say that Jellybean doesn't need an internet connection? It's that it doesn't requires the PERMISSION on Manifest (but still requires a connection) or it doesn't need Internet connection? 'Cause i couldn'nt make it work without connection! (4.1.2 here)

    ReplyDelete
  2. Very useful information that you have shared and it is very useful to me.Thanks for sharing the information with us.

    mobile app development company in chennai

    ReplyDelete
  3. very well explained thanks for sharing your valuable blog these are very useful to me.
    Speech to Text

    ReplyDelete
  4. Very significant Information for us, I have think the representation of this Information is actually superb one. This is my first visit to your site. Artificial intelligence software

    ReplyDelete
  5. Thanks for sharing the nice information. you have a need speech to text services. Sapphire is a most-famous speech to text app development services provider India, USA, Australia, Canada and more.

    ReplyDelete

Build a Custom Kernel Module for Android

Hi Guys!!!Hope you are doing well !!!. Today I will describe how you can write a custom kernel module(Hello world) for Android and load it a...