Sei sulla pagina 1di 19
Go to Go to Latest Post Android implementing Home » Android Programming » Android Speech
Go to
Go to
Latest Post
Android implementing
Home » Android Programming » Android Speech Recognition – Example

Are you a developer? Try out the HTML to PDF API

Android Speech Recognition – Example
Android Speech Recognition – Example

Are you a developer? Try out the HTML to PDF API

Speech Recognition is used to convert user’s voice to text. In this tutorial we are
Speech Recognition is used to convert user’s voice to
text. In this tutorial we are going to implement Google
Speech Recognition in our Android Application which
will convert user’s voice to text and it will display it in
TextView.
Creating Project
Make sure you have properly setup the Android SDK,
AVD for Testing the Application. Its better to have a
physical device for testing. Create a New project in
Eclipse IDE with the package as
com.learn2crack.speech. Create the Main Activity as MainActivity and the main Layout as
activity_main.
Download Complete Project Speech Recognition SpeechRecognition.zip Version: 1.0 1.4 MiB 5 Downloads DETAILS
Download Complete Project
Speech Recognition
SpeechRecognition.zip
Version: 1.0
1.4 MiB
5 Downloads
DETAILS

Are you a developer? Try out the HTML to PDF API

Creating Layout

The main layout of our project activity_main contains a Button to Start Google Speech Recognition

and a TextView to display the converted voice.

activity_main.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" tools:context=".MainActivity" > <TextView android:id="@+id/welcome" android:layout_width="wrap_content" android:layout_height="wrap_content"

android:textSize="30sp"

android:layout_gravity="center" android:text="Speech Recognotion" /> <Button android:id="@+id/start_reg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Start Speech Recognition" /> <TextView android:id="@+id/speech"

android:textSize="25sp"

android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> </LinearLayout>

Are you a developer? Try out the HTML to PDF API

We have another layout dialog_matches_frag which shows a list of matching voice to text convertions

from the input.

dialog_matches_frag.xml

<?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" > <ListView android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>

Creating Activity

In our MainActivity import the layout items. The fuction isConnected() is used to check whether the

device has a active Internet connection, Since voice recognition will only work with working Internet

connection. If it has a working Internet connection the Speech Recognizer Intent will be opened and it

will be listening for voice. When it successfully recognises the voice it responses with RESULT_OK. The

we are opening a new Dialog to show the matched text in listview. When user selects a item from the

list it is displayed in TextView. The dialog is initialised by new Dialog(MainActivity.this).

Are you a developer? Try out the HTML to PDF API

MainActivity.java package com.learn2crack.speech; import java.util.ArrayList; import android.net.ConnectivityManager;
MainActivity.java
package com.learn2crack.speech;
import java.util.ArrayList;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.speech.RecognizerIntent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final int REQUEST_CODE = 1234;
Button Start;
TextView Speech;
Dialog match_text_dialog;
ListView textlist;
ArrayList<String> matches_text;
@Override
protected void onCreate(Bundle savedInstanceState) {

Are you a developer? Try out the HTML to PDF API

super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Start = (Button)findViewById(R.id.start_reg); Speech = (TextView)findViewById(R.id.speech); Start.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if(isConnected()){

Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); startActivityForResult(intent, REQUEST_CODE);

}

else{ Toast.makeText(getApplicationContext(), "Plese Connect to Internet", T

}

});

}}

public boolean isConnected()

{

ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNEC NetworkInfo net = cm.getActiveNetworkInfo(); if (net!=null && net.isAvailable() && net.isConnected()) { return true; } else { return false;

}

}

@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) { match_text_dialog = new Dialog(MainActivity.this); match_text_dialog.setContentView(R.layout.dialog_matches_frag); match_text_dialog.setTitle("Select Matching Text"); textlist = (ListView)match_text_dialog.findViewById(R.id.list); matches_text = data .getStringArrayListExtra(RecognizerIntent.EXTRA RESULTS);

Are you a developer? Try out the HTML to PDF API

_ ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
_
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, matches_text);
textlist.setAdapter(adapter);
textlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Speech.setText("You have said " +matches_text.get(position));
match_text_dialog.hide();
}
});
match_text_dialog.show();
}
super.onActivityResult(requestCode, resultCode, data);
}
}
Creating Manifest
We need the permission to access Internet and Network State.
android.permission.INTERNET
android.permission.ACCESS_NETWORK_STATE
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.learn2crack.speech"
android:versionCode="1"
android:versionName="1.0" >

Are you a developer? Try out the HTML to PDF API

<uses-sdk android:minSdkVersion="15" android:targetSdkVersion="19" /> <uses-permission
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.learn2crack.speech.MainActivity"
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>
Finally run the project in emulator or in a physical device.
Screenshots
Enjoy Any questions comment here. ► Android APS ► Android CRM ► Android 4G ►
Enjoy
Any questions comment here.
► Android APS ► Android CRM ► Android 4G
► Android Pit

Are you a developer? Try out the HTML to PDF API

Share ! 1 19 Share About Raj Amal Raj Amal is a Co-founder of Learn2crack.com.
Share !
1
19
Share
About Raj Amal
Raj Amal is a Co-founder of Learn2crack.com. He is a Android Developer and Blogger.
Previous:
Next:
»
« Android Swipe View with Tab layout
– Example
Android implementing Location
Services – Example
Related Articles
Android implementing
Location Services –
Example
Android Swipe View with
Tab layout – Example
Android Google Plus API –
Example
December 13, 2013
December 12, 2013
December 15, 2013
0 comments

Are you a developer? Try out the HTML to PDF API

Start the discussion… Best Community Share No one has commented yet. ALSO ON LEARN2CRACK Android
Start the discussion…
Best
Community
Share
No one has commented yet.
ALSO ON LEARN2CRACK
Android WebView – Example
Android Custom ListView with Images an
Example
8 comments • 3 months ago
Tek Raj — blank came.
18 comments • 2 months ago
dashar sang — In the CustomList.java shou
constructorpublic CustomList(Activity conte
web, Integer[] imageId)match …
Setup Apache Web Server with PHP, MySQL in
Raspberry Pi
Apps Development
2 comments • 3 months ago
8 comments • 2 months ago
Raj Amal — Yes you can
Srini Vasan — thanks for commenting.

Are you a developer? Try out the HTML to PDF API

Subscribe Add Disqus to your site Search for: Subscribe 109 To RSS Feed Followers 2,238
Subscribe
Add Disqus to your site
Search for:
Subscribe
109
To RSS Feed
Followers
2,238
190
Fans
Subscribers
Subscribe to Us
Email Address
Subscribe

Are you a developer? Try out the HTML to PDF API

Find us on Facebook Learn2Crack Like 2,247 people like Learn2Crack. Facebook social plugin Follow us
Find us on Facebook
Learn2Crack
Like
2,247 people like Learn2Crack.
Facebook social plugin
Follow us on Google+

Are you a developer? Try out the HTML to PDF API

Recent Posts Android implementing Location Services – Example Android Speech Recognition – Example Android Swipe
Recent Posts
Android implementing Location Services – Example
Android Speech Recognition – Example
Android Swipe View with Tab layout – Example
Android Google Plus API – Example
How to Setup Google Play services and Obtain SHA1 fingerprint
How To Change The SubTitle Font Colour
Android Google Maps API v2 – Example
How to pull SQLite database file from Android device
Android Spinner Dropdown – Example
How To Install And Use BBM On PC Using Bluestacks

Are you a developer? Try out the HTML to PDF API

Tags Learn2Crack on Twitter Tweets by @Learn2Crack 4.3 ADB Android Android apk Android Games Apps
Tags
Learn2Crack on Twitter
Tweets by @Learn2Crack
4.3 ADB Android Android apk Android
Games Apps BBM decompile Facebook Firefox Os
Free Domain Free Download Free Hosting
Genymotion Github Google Play Google Products
Hack Internet Jolla Sailfish Kernel Linux ListView
Local Webserver Lock Screen Login Mobile OS Nexus 7 Phone
Concepts Phone Gap PPA Premium Sites Proxy Server
Raspberry Pi Registration SDk Surf Tech Products

Are you a developer? Try out the HTML to PDF API

Terminal Torrent Transfer Files Tricks Ubuntu VNC Server Windows Pages Advertise Apps Development Contact Us
Terminal Torrent Transfer Files Tricks
Ubuntu VNC Server Windows
Pages
Advertise
Apps Development
Contact Us
Disclaimer
Privacy Policy
Videos
Copyright © 2013 Learn2Crack
All Rights Reserved

Are you a developer? Try out the HTML to PDF API