RSS

Voice To Text on Android

09 Aug

This is a code snippet in response to a request on how to recognise voice of a person. This is useful for the translator app, I built in some previous tutorial.

package com.gilo.voice;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class MainActivity extends Activity {

	Button speak;
	ListView options;
	ArrayList<String> results;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		speak = (Button) findViewById(R.id.bSpeak);
		options = (ListView) findViewById(R.id.lvOptions);

		speak.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// This are the intents needed to start the Voice recognizer
				Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
				i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
						RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
				i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Say something");

				startActivityForResult(i, 1010);
			}
		});

		// retrieves data from the previous state. This is incase the phones
		// orientation changes
		if (savedInstanceState != null) {
			results = savedInstanceState.getStringArrayList("results");

			if (results != null)
				options.setAdapter(new ArrayAdapter<String>(this,
						android.R.layout.simple_list_item_1, results));
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		// retrieves data from the VoiceRecognizer
		if (requestCode == 1010 && resultCode == RESULT_OK) {
			results = data
					.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
			options.setAdapter(new ArrayAdapter<String>(this,
					android.R.layout.simple_list_item_1, results));
		}

		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		// This should save all the data so that when the phone changes
		// orientation the data is saved
		super.onSaveInstanceState(outState);

		outState.putStringArrayList("results", results);
	}

}

And for the xml file

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/lvOptions"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/button1" >
    </ListView>

    <Button
        android:id="@+id/bSpeak"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="Speak" />

</RelativeLayout>

Screenshot_2013-08-09-08-22-09

Screenshot_2013-08-09-08-24-06

Advertisements
 
9 Comments

Posted by on August 9, 2013 in Uncategorized

 

9 responses to “Voice To Text on Android

  1. vallabh

    March 15, 2014 at 5:03 pm

    ohhhh dear its so cool …….
    can i get the source code for this app 🙂

     
  2. Inah Claudine Domingo

    June 19, 2014 at 2:16 am

    can i get the source code and use for a case study?

     
    • Gilbert Kimutai

      July 24, 2014 at 9:47 am

      Sorry for the long delay. Sure thing. Let me know how the study goes or went.

       
      • iand

        September 24, 2014 at 3:18 pm

        please provide me the source code for example of my work, now I working on translation project for my research thesis..thanks anyway
        developadi@gmail.com

         
      • Gilbert Kimutai

        October 6, 2014 at 5:17 pm

        The code is availed somewhere in this blog. I will have to look for it. I wrote this article a long time ago.

         
  3. Mapula

    October 3, 2014 at 2:14 pm

    hi, i have so much errors on the code I don’t know where to place the codes pls help

     
    • Gilbert Kimutai

      October 6, 2014 at 5:15 pm

      Its probably something small. Persevere on and maybe you’ll get some breakthrough

       
  4. hana

    October 10, 2014 at 11:27 am

    how can i write this code and translator app in one project ,, please give me explane ..

     
  5. hana

    October 10, 2014 at 12:20 pm

    the intent is not found :
    Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
    RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    i.putExtra(RecognizerIntent.EXTRA_PROMPT, “Say something”);
    help me!

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: