Вы находитесь на странице: 1из 19

Go to...

Go to...

Latest Post

Android implementing

Home Android Programming Android Speech Recognition Example

open in browser PRO version

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

pdfcrowd.com

Android Speech Recognition Example

open in browser PRO version

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

pdfcrowd.com

Speech Recognition is used to convert users voice to text. In this tutorial we are going to implement Google Speech Recognition in our Android Application which will convert users 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.

open in browser PRO version

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

pdfcrowd.com

Download Complete Project


Speech Recognition
SpeechRecognition.zip Version: 1.0 1.4 MiB 5 Downloads DETAILS

open in browser PRO version

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

pdfcrowd.com

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>
open in browser PRO version
Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

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).

open in browser PRO version

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

pdfcrowd.com

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) {
open in browser PRO version
Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

protected void onCreate(Bundle savedInstanceState) { 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", Toast.LENGT }} });

public boolean isConnected() { ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SER 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

open in browser PRO version

pdfcrowd.com

});

.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 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" >
open in browser PRO version
Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

android:versionName="1.0" > <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

open in browser PRO version

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

pdfcrowd.com

open in browser PRO version

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

pdfcrowd.com

Enjoy Any questions comment here.


Android APS Android CRM Android 4G Android Pit

open in browser PRO version

Share !

19

Share

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

pdfcrowd.com

Share !

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


December 15, 2013

Android Swipe View with Tab layout Example


December 13, 2013

Android Google Plus API Example


December 12, 2013

0 comments
open in browser PRO version
Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

0 comments Start the discussion


Best Community Share

No one has commented yet.

ALSO ON LEARN2CRACK

Android WebView Example


8 comments 3 months ago

Android Custom ListView with Images and Text Example


18 comments 2 months ago

Tek Raj blank came.

dashar sang In the CustomList.java shouldn't

constructorpublic CustomList(Activity context,String[] web, Integer[] imageId)match

Setup Apache Web Server with PHP, MySQL in Raspberry Pi


8 comments 2 months ago

Apps Development
2 comments 3 months ago

Raj Amal Yes you can

Srini Vasan thanks for commenting.


open in browser PRO version
Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

Subscribe

Add Disqus to your site

Search for:

Subscribe
To RSS Feed

Followers

109

2,238
Fans

Subscribers

190

Subscribe to Us
Email Address

Subscribe
open in browser PRO version
Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

Find us on Facebook
Learn2Crack
Like

2,247 people like Learn2Crack.

Facebook social plugin

Follow us on Google+

open in browser PRO version

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

pdfcrowd.com

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
open in browser PRO version
Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

Tags
4.3 ADB
Games

Learn2Crack on Twitter

Android Android apk

Tweets by @Learn2Crack
Android Firefox Os

Apps BBM decompile Facebook

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


open in browser PRO version
Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

Raspberry Pi Registration
Terminal Torrent Transfer Files

Surf

Tricks

Ubuntu VNC Server Windows

Pages
Advertise Apps Development Contact Us Disclaimer Privacy Policy Videos

Copyright 2013 Learn2Crack

All Rights Reserved

open in browser PRO version

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

pdfcrowd.com