Tuesday, 25 August 2015

Android Programming Tutorials 8 : Voice (Speech) Recognizer in Android:

Speech Reconizer:

Speech or Voice Recognizer in the name itself means that it can recognize our speech.


OutPut Of the Tutorial:




How it will work:
In this tutorial we just using some predefined services provided by Google for recognization purpose. Just click on the button and speak what ever you want.

Create Android Application:

  • File >> New >> Android Application
  • Enter Project name: VoiceApp
  • Package: com.ambilpursunil.newapp
  • Keep other default selections, click Next until you reach Finish

                       


2.Copy and past the following code in the  MainActivity.java 

packag com.ambilpursunil.newapp;

import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognitionListener;
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 Voice extends Activity implements OnClickListener {
                         ListView lv;
                        static final int check = 111;

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                        // TODO Auto-generated method stub
                        super.onCreate(savedInstanceState);
                        setContentView(R.layout.voice);
                        lv = (ListView) findViewById(R.id.lvVioceReturn);
                        Button b = (Button) findViewById(R.id.bVoice);
                        b.setOnClickListener(this);
            }
            @Override
            public void onClick(View v) {
                        // TODO Auto-generated method stub
Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                        i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                        RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
              i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak On....! ");
                        startActivityForResult(i, check);
            }

            @Override
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                        // TODO Auto-generated method stub
                        if (requestCode == check && requestCode == RESULT_OK) {
ArrayList<String> result = data                    .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                        lv.setAdapter(new ArrayAdapter<String>(this,
                        android.R.layout.simple_list_item_1, result));
                        }
                        super.onActivityResult(requestCode, resultCode, data);
            }
   }

2.Make the change the of  activity_main.xml  file as the below


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/bVoice"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Button" />

    <ListView
        android:id="@+id/lvVioceReturn"
        android:layout_width="match_parent"
        android:layout_height="fill_parent" >
    </ListView>

</LinearLayout>


3.Copy and past the code for  manifest.xml  file

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true" 
        android:icon="@drawable/ic_launcher" 
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

    <activity 
       android:name="com.ambilpursunil.newapp.Voice"
       android:configChanges="keyboard|keyboardHidden|orientation>

   <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>

       </activity>
  </application>
</manifest>

4.Right click on the project and Run As-->Android Application




Just click on the button and speak...


Please Send Your Comments To ambilpura.sunil@gmail.com


Stay Tune For Next Tutorial... TextVoice in Android:

No comments:

Post a Comment