Android Speech Recognition – Example

Embed Size (px)

Citation preview

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    Go to...

    Go to...

    Latest Post Android implementing

    Home Android Programming Android Speech Recognition Example

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    Android Speech Recognition Example

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    Speech Recognition is used to convert users voice totext. In this tutorial we are going to implement GoogleSpeech Recognition in our Android Application whichwill convert users voice to text and it will display it inTextView.

    Creating Project

    Make sure you have properly setup the Android SDK,AVD for Testing the Application. Its better to have aphysical device for testing. Create a New project inEclipse IDE with the package as

    com.learn2crack.speech. Create the Main Activity as MainActivity and the main Layout asactivity_main.

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    Speech Recognition SpeechRecognition.zipVersion: 1.0

    1.4 MiB5 Downloads

    DETAILS

    Download Complete Project

  • pdfcrowd.comopen in browser PRO version 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 Recognitionand a TextView to display the converted voice.

    activity_main.xml

  • pdfcrowd.comopen in browser PRO version 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 convertionsfrom the input.

    dialog_matches_frag.xml

    Creating Activity

    In our MainActivity import the layout items. The fuction isConnected() is used to check whether thedevice has a active Internet connection, Since voice recognition will only work with working Internetconnection. If it has a working Internet connection the Speech Recognizer Intent will be opened and itwill be listening for voice. When it successfully recognises the voice it responses with RESULT_OK. Thewe are opening a new Dialog to show the matched text in listview. When user selects a item from thelist it is displayed in TextView. The dialog is initialised by new Dialog(MainActivity.this).

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    MainActivity.javapackage 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 matches_text; @Override protected void onCreate(Bundle savedInstanceState) {

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    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.LENGTH_LONG).show(); }} });} public boolean isConnected() { ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 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);

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    Creating Manifest

    We need the permission to access Internet and Network State.

    android.permission.INTERNETandroid.permission.ACCESS_NETWORK_STATE

    AndroidManifest.xml

    .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); ArrayAdapter adapter = new ArrayAdapter(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); }}

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    Finally run the project in emulator or in a physical device.

    Screenshots

    android:versionName="1.0" >

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF APIShare ! 19 1

    Enjoy Any questions comment here.

    Android APS Android CRM Android 4G Android Pit

    Share

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    Share ! 19 1

    About Raj Amal

    Related Articles

    Android implementingLocation Services Example

    December 15, 2013

    Android Swipe View withTab layout Example

    December 13, 2013

    Android Google Plus API Example

    December 12, 2013

    Raj Amal is a Co-founder of Learn2crack.com. He is a Android Developer and Blogger.

    0 comments

    Share

    Previous:

    Android Swipe View with Tab layout Example

    Next:Android implementing LocationServices Example

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    0 comments

    ALSO ON LEARN2CRACK

    Android WebView Example8 comments 3 months ago

    Tek Raj blank came.

    Android Custom ListView with Images and Text Example18 comments 2 months ago

    dashar sang In the CustomList.java shouldn'tconstructorpublic CustomList(Activity context,String[]web, Integer[] imageId)match

    Setup Apache Web Server with PHP, MySQL inRaspberry Pi8 comments 2 months ago

    Srini Vasan thanks for commenting.

    Apps Development2 comments 3 months ago

    Raj Amal Yes you can

    Start the discussion

    Best Community Share

    No one has commented yet.

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    Subscribe Add Disqus to your site

    SubscribeTo RSS Feed

    109Followers

    2,238Fans

    190Subscribers

    Subscribe to Us

    Search for:

    Email Address

    Subscribe

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    Find us on Facebook

    Follow us on Google+

    Learn2Crack

    2,247 people like Learn2Crack.

    Facebook social plugin

    Like

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    Recent PostsAndroid 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

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    Tags

    4.3 ADB Android Android apk AndroidGames Apps BBM decompile Facebook Firefox OsFree Domain Free Download Free HostingGenymotion Github Google Play Google Products

    Hack Internet Jolla Sailfish Kernel Linux ListViewLocal Webserver Lock Screen Login Mobile OS Nexus 7 PhoneConcepts Phone Gap PPA Premium Sites Proxy Server

    Raspberry Pi Registration SDk Surf Tech Products

    Learn2Crack on Twitter

    Tweets by @Learn2Crack

  • pdfcrowd.comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    Raspberry Pi Registration Surf

    Terminal Torrent Transfer Files TricksUbuntu VNC Server Windows

    Pages

    Advertise

    Apps Development

    Contact Us

    Disclaimer

    Privacy Policy

    Videos

    All Rights ReservedCopyright 2013 Learn2Crack