Professional Documents
Culture Documents
Go to...
Latest Post
Android implementing
pdfcrowd.com
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.
pdfcrowd.com
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).
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
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(); }
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
pdfcrowd.com
pdfcrowd.com
Share !
19
Share
pdfcrowd.com
Share !
19
Share
Previous:
Next:
Related Articles
0 comments
open in browser PRO version
Are you a developer? Try out the HTML to PDF API
pdfcrowd.com
ALSO ON LEARN2CRACK
Apps Development
2 comments 3 months ago
pdfcrowd.com
Subscribe
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
Follow us on Google+
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
Tweets by @Learn2Crack
Android Firefox Os
Free Domain
pdfcrowd.com
Raspberry Pi Registration
Terminal Torrent Transfer Files
Surf
Tricks
Pages
Advertise Apps Development Contact Us Disclaimer Privacy Policy Videos
pdfcrowd.com