text: I’ve been searching for a simple tutorial on using voice recognition in Android but haven’t had much luck. The Google official documentation provide an example of the activity, but don’t speculate anything more than that, so you’re kind of on your own a little.
  Luckily I’ve gone through some of that pain, and should make this easy for you, post up a comment below if you think I can improve this.
  I’d suggest that you create a blank project for this, get the basics, then think about merging VR into your existing applications. I’d also suggest that you copy the code below exactly as it appears, once you have that working you can begin to tweak it.
  With your blank project setup, you should have an AndroidManifest file like the following
  <?xml version="1.0" encoding="utf-8"?> <manifest xmlns: <application android: <activity android: <intent-filter> <action android: <category android: </intent-filter> </activity> </application> </manifest>
  And have the following in res/layout/voice_recog.xml :
  <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns: <TextView android: <Button android: <ListView android: </LinearLayout>
  And finally, this in your res/layout/main.xml :
  <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns: <TextView android: </LinearLayout>
  So thats your layout and configuration sorted. It will provide us with a button to start the voice recognition, and a list to present any words which the voice recognition service thought it heard. Lets now step through the actual activity and see how this works.
  You should copy this into your activity :
  package com.jameselsey; import android.app.Activity; import android.os.Bundle; import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.speech.RecognizerIntent; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import java.util.ArrayList; import java.util.List; /** * A very simple application to handle Voice Recognition intents * and display the results */ public class VoiceRecognitionDemo extends Activity { private static final int REQUEST_CODE = 1234; private ListView wordsList; /** * Called with the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.voice_recog); Button speakButton = (Button) findViewById(R.id.speakButton); wordsList = (ListView) findViewById(R.id.list); // Disable button if no recognition service is present PackageManager pm = getPackageManager(); List<ResolveInfo> activities = pm.queryIntentActivities( new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0); if (activities.size() == 0) { speakButton.setEnabled(false); speakButton.setText("Recognizer not present"); } } /** * Handle the action of the button being clicked */ public void speakButtonClicked(View v) { startVoiceRecognitionActivity(); } /** * Fire an intent to start the voice recognition activity. */ private void startVoiceRecognitionActivity() { Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Voice recognition Demo..."); startActivityForResult(intent, REQUEST_CODE); } /** * Handle the results from the voice recognition activity. */ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) { // Populate the wordsList with the String values the recognition engine thought it heard ArrayList<String> matches = data.getStringArrayListExtra( RecognizerIntent.EXTRA_RESULTS); wordsList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, matches)); } super.onActivityResult(requestCode, resultCode, data); } }
  Breakdown of what the activity does :
  Declares a request code, this is basically a checksum that we use to confirm the response when we call out to the voice recognition engine, this value could be anything you want. We also declare a ListView which will hold any words the recognition engine thought it heard.
  The onCreate method does the usual initialisation when the activity is first created. This method also queries the packageManager to check if there are any packages installed that can handle intents for ACTION_RECOGNIZE_SPEECH. The reason we do this is to check we have a package installed that can do the translation, and if not we will disable the button.
  The speakButtonClicked is bound to the button, so this method is invoked when the button is clicked. I wrote another tutorial on button binding so have a look at that if you don’t understand this method.
  The startVoiceRecognitionActivity invokes an activity that can handle the voice recognition, setting the language mode to free form (as opposed to web form)
  The onActivityResult is the callback from the above invocation, it first checks to see that the request code matches the one that was passed in, and ensures that the result is OK and not an error.
  Next, the results are pulled out of the intent and set into the ListView to be displayed on the screen.
  Notes on debugging :
  You won’t have a great deal of luck running this on the emulator, there may be ways of using a PC microphone to direct audio input into the emulator, but that doesn’t sound like a trivial task. Your best bet is to generate the APK and transfer it over to your device (I’m running an Orange San Francisco / ZTE Blade).
  If you experience any crashing errors (I did), then connect your device via USB, enable debugging, and then run the following command from a console window
  <android-home>/platform-tools/adb -d logcat
  What this does, is invoke the android device bridge, the -d switch specifies to run this against the physical device, and the logcat tells the ADB to print out any device logging to the console.
  This means anything you do on the device will be logged into your console window, it helped me find a few null pointer issues.
  That’s pretty much it, its quite a simple activity. Have a play with it and if you have any comments please let me know. You may have mixed results with what the recognition thinks you have said, I’ve had some odd surprises, let me know!
  Happy coding!
  98 thoughts on “Android; How to implement voice recognition, a nice easy tutorial…”
  Hi James! This is a brilliant tutorial, at least now I understand the code for speech recognition with android. However it shows “recognizer not present” when I run it. What can be the problem? I have a C#speech recognition’s application that work properly on the same machine.
  Is there a setting that I should make in order to enable the recognizer for android?
  Thank you
  Patrick
  Hi Patrick, glad I’ve helped.
  When you say you have C# speech recognition, what do you mean by that? Something installed on your local PC? Are you running this on the emulator or on a device?
  thanks
  Thanks for the quick response.
  I meant that I am able to use the Windows 7 Speech recogniser via a C# application. I was hoping that it will be the case with an android application using the emulator. But I just read that the SDK does not include the speech recognition and that it should work on a device, I don’t have a device yet.
  It there any thing that I can do to attach the speech recogniser to my SDK so that I can use it with my emulator?
  Thanks
  Hi James,
  Thanks for the very good tutorial. I don’t know if its possible to do without getting into unpublished methods within the RecognizerIntent, but I’m trying to run the VR in a service that is interval controlled, and most important DOES NOT SHOW A PROMPT. Do you have any suggestions or can you steer me to a resource that might help?
  Thanks
  Hi Dave,
  Thank you for the kind words, glad I’ve helped. So you want to start off VR from a service that is interval controlled, can I assume that by interval controlled you mean once every X number of seconds for example?
  Any reason why you specifically need a service for this and why you can’t stick with a basic activity? In regards to not showing a prompt, I’m not sure we have much control over that since its part of the droid SDK, but there may be a way of masking that out, I’ll do some digging.
  Cheers
  I guess I can use an Audio Sensor (do you know of a good tutorial, I got errors on Frank Ableson’s tutorial, still can’t get it to work)
  If there is no way to override the RecognizerIntent class I will have to write my own using the MediaRecorder, ugh.
  I Need to run as a persistent service for his app, it never completely stops and yes, I will be controlling the interval the VR runs, and stops.
  Interesting, well heres what I think..
  Create a service to do the VR, then have your activity start/stop this based on a time interval. Inside the service you will need to fire off intents to the VR action. I’m not sure how you’ll keep the VR open for extended periods of time (since if it doesn’t hear anything after x number of seconds it will fail).
  To cover the default prompt you may have to show a view on top of it to mask it out (i.e., show a splash screen over it).
  I’d have to dabble in a bit more code to give any better advice :)
  Hey, I tried the code on a recent samsung powered with android but it says the same “recognizer not present”. How did you manage to get it working? Is there specific settings?
  Hi Patrick, this means that your handset doesn’t have an voice recognition engines. You can go onto the market place and download google Voice if you don’t already have it.
  My app makes use of VR engines, but it doesn’t provide one itself
  Hope this helps
  Hi James.
  When I try to run the app on the emulator but it says recognizer not present
  Hi Simphiwe,
  This means that you don’t have any voice recognition engines on your handset, go onto the marketplace and download Google Voice.
  My app makes use of VR engines, but it doesn’t provide one itself.
  Hope this helps
  hai,
  thank you for your tutorial. i am trying to run this code using my own database. i prepared my language and acoustic models for the arabic language but I do not know how to connect my database with this code.
  my application is samall it includes only 10 words(i say the number in arabic and it writes it down).
  the source i am using for the training is a jar file and it saves my results in zip files.
  Hi Amal,
  Interesting app you have there!
  What behaviour are you experiencing? I think you might have problems using Arabic, this is what the Google website says :
  So I think that possibly Arabic is not support yet.
  hi amal,
  I am trying to do the same thing…so did you successeded?
  I need help
  thanks
  Thanks for the tutorial. The tutorial on the android dev website din’t help much.
  Thanks
  Your tutorial was wonderful and worked like a charm, I have a question about VR. Is it possible to input 1 sound and receive another? For example : I input the phrase “I want to go to sleep” and the VR exports the phrase “then go lay down” or “it’s not your bedtime yet”. So i want to input one value/word and have the VR export another desired value. Is that possible? and if so can you point me in the right direction?
  You can absolutely do that! However there will be some difficulties along the way. How should the app know how to respond? Will you have a database of phrases, whereby if one is detected, it will know the mapping to the response? Or are you planning some form of Artificial Intelligence to do this?
  Also, the VR can be a little sketchy depending on accents and so forth, I tend to mumble a little bit and it often has trouble detecting what I’m saying.
  Interesting idea you have though, I’d like to hear how it goes :)
  Thanks for the speedy response! To answer your questions “How should the app know how to respond? Will you have a database of phrases, whereby if one is detected, it will know the mapping to the response? Or are you planning some form of Artificial Intelligence to do this?” … I don’t know yet. I was thinking of adding a database of catch words, whereas if input is “A” export is “Z”, but I don’t know how to set the Method to do that yet. I mean if i could just learn how to set the first method, i could figure the rest out. Or if i the format for manipulating the database was present, i would at least have a starting point. Any help?
  Thanks in advance.
  Sounds a bit fiddly, you could store phrases in a database, then when one is spoken you can obtain all the words and see how many of those match the set phrases you have, for example if 8 out of the 10 words match, you’d be pretty confident. Thats just one way of doing it.
  The technical side should be fairly easy, just reading/writing to a SQLite instance, the tricky part is working out the business logic of how you want it to work.
  Personally (without knowing any more about what your app does) I would store set phrases such as “Hello my name is ?” and then when it hears “Hello, my name is James”, it would recognise the words “Hello”, “my”, “name”, “is”, “James” and map them to a set response. The name James would be detected as a variable and used in the response “Nice to meet you James”.
  Good luck!
  So does that mean that I have to “build the database”, “modify a built in database” or direct the app to “a database”?
  Yeah, create yourself a database, a table, and populate it with some dummy data. Please checkout the SQLite tutorial I did, it covers pretty much all of the aforementioned.
  You can use an online database, or a local SQLite db, or even sync the two, its up to you and depends if you need offline modes.
  Hi James .
  In my case i have just copied the existing VoiceRecognitionDemo code you have given here and i am running the code through device but after clicking on button i am getting connect error ..
  just to mention Google voice search is installed on phone and running perfectly
  Please help
  Connect error? Sounds like you don’t have internet connection, which you would need for the VR to work.
  Couple of things, check your internet connectivity, make sure manifest has internet permissions.
  If that still doesn’t work, put your logs into pastebin.com and give me a link! :)
  yeah yes it worked that time only..i refered some blog where it was mentioned that we need to have nternet for google voice search as it has to connect google data server..
  anyways thanks for quick reply
  That is true, everytime you speak into the app, it collects that audio data and sends it off to Google for them to do their “crunching” and then replies with the text, so you must have internet connectivity.
  Happy coding :)
  Hi James, I was just wondering how long does it take to recognize a word if it has to go to google servers 1st. how many bytes get send and received per word? Is there any way you can use this class without internet access?
  Hi Bennie,
  It depends really, it depends on how many words you are processing, for example if you are quoting 2 pages from a book it will take a bit longer!
  Also depends on your internet connection, if you have really slow connection the data transfer will obviously take longer.
  No idea about how many bytes, haven’t been concerned with that low level stuff.
  If you want to use the Google VR engine then you’ll need internet access, I don’t believe there is a way around that. However there may be other 3rd party VR engines that work in offline mode, I’m not too sure.
  Hope this helps
  Hey James,
  Thanks for the great tutorial!
  Unfortunately I get a “recognizer not present” message on my phone. (Also ZTE Blade)
  This is probably due to the fact that for some reason Google Voice is not supported in my country for some reason. (Israel)
  I wonder if I can somehow get another speech recognizer to work, maybe you know about another application?
  I’m a total Android noob btw.
  Thanks!
  hi James ,i am from India , thats why i’m unable to use Google voice &therefore i’m getting error”recognizer not present” Please suggest me some better option
  hi james,i am from india,am doing my academic project in one of MNC company.I want to impliment biometric application in android mobile phones.Through voice recognition my mobile should be unlocked, it shld be unlocked for my own voice.so can u suggest code releted to my project.
  hi james,
  Is it possible ot make this app work offline?? without google voice ? (with my own database)
  thanks
  Thank you very much for your effort.
  Could you please provide your Facebook account so that we can communicate with you?
  Best Regards,
  Mahmoud
  How i can run speech recognizer… though USB….
  Give detail for me please….!!!!!
  Another real useful tut from u :)
  y james replay for my quari?
  y james no reply for my quari??
  Hey! I would like to know if there is a way of not using the speech server (Voice search).
  The Voice dialler apparently use an engine which is on the phone. I have seen a couple of app that do too.
  Thanx
  Hello, brilliant tutorial! I get one error though :( “Can’t dispatch DDM chunk 52454151: no handler defined” I have tried everything to fix this! Any chance you can send me the .apk of yours so I can see what is different? Thank you in advance!
  Interesting, which version of the android SDK are you working to?
  Hi James,brilliant tutorial. I’m new to speech recognition and Android, and would like to know how is a database for a speech app created ( i.e. if you are creating your own db, say of 10 words only) and how is it connected to the app?
  hi James . it is possible to do that what you think in your mind it should want to be happen in the mobile?
  Hi James, brilliant tutorial indeed. I am new to Android and speech recognition and would like to know how is a database of a speech recognition app (i.e. an app working without google search or offline) built and how are the two(app & db) connected. do have any idea? please help
  Hi Chepe, I’m not too sure since it is not something I’ve investigate too much (offline TTS that is) however there is more than likely APIs out there which you can include in your app. App/DB interconnectivity will probably be via SQLite if local, or some kind of web service call if you have a centralised base on the web.
  Hope this helps :)
  Hi James, i’m trying to implement text to speech in our language. Can you give me the right method to produce the voice/sounds from database?? thanks in advance
  Hi,
  Which language are you wanting to use, Philipino? Chinese? Spanish?
  Thanks
  philipino…
  i’m from Philippine not UK..
  Philipino…We want to know what are the processes in putting natural voices in android so that we can make text-to-speech in philipino.. we are trying to use Syllabification Algorithm..thanks..
  Philipino. we are using syllable algorithm..and we have also problems in browsing and receiving text messages..i hope you will help us..thanks
  Hi James, Great tutorial :).
  Can I use this engine to recognize sounds of an object rather then words (for example hearing a car or hearing a keyboard pressed)?
  I want to identify the objects by the sound
  Or I have to develop my own engine that do so?
  Great Tutorial! I Was wondering if i could have the user say something and it would search a site in the provided webview of my app.
  Hi James ,
  Nice Tutorial …
  Wondering if you can upload somewhere the the “apk” so that i can test it on my device before playing with the codes …
  Thanks , will be of great help if u can do so asap
  Jacobs
  hi ,we not have google voice service in our country . So there is another way to speech recognition on emulator?
  thank you .nice tutorial ..chennai ..india
  pls update with new android tech tutorials as well
  like phone gap
  why is it that if i say good morning even then it shows me a list good morning strings added with boarding govin body boring..so on listed..
  is there a wayh to filter out and list out the exact words spoken out..
  pleas reply for the same..thank u..
  Hi james,
  thax for your tutorial ,its very useful for beginnig speech recognition.I ask you this:when I debug it activities (list parameter) size null ,so run if clause directly.This is a simple mistake about hardware or something ,I think.What about you?
  Thanks for this tutorial. But how to implement “RecognizerIntent.EXTRA_WEB_SEARCH_ONLY” in the onActivityResult method? Thanks in advance!!!
  hi james..
  how can i add speech recognizer in my emulator ?????? i have no android phone……through emulator i am trying to download voice recognizer bt failed … i also used update sdk 3.2 bt failed ………plz help me……….i need a detail help.
  Hi Auvy, I’m not quite sure, as you would need some way of sending voice input into the emulator. There must be a way via the ADB, but I’m not entirely sure, you’ll have to Google for it. If all else fails, try asking on StackOverflow.com, its a great site for quick and detailed answers. Good luck!
  Hi James,
  Thanks for the tutorial. I have it almost working.
  First it wasn’t working at all, because I had no speech recognizer. So I installed voice search and now the button is visible. When I click on the button it starts. But then once I say anything the program crashes. I get the following error:
  “The application Voice Search (process com.google.android.voicesearch) has stopped unexpectedly. Please try again. – Force close.
  Any ideas as to what I should do?
  Kind Regards
  Aubrey.
  Ahh,
  Fixed it! This will answer the question for allot of people here who want to know how to get a “voice search” speech recognizer other than google voice.
  Just go to Android market and download an app called “Jeannie”. When you start the app it will say you need to download a speech recognizer. It gives you an option to install voice search for android 1.5 or android 2.1. Then your app works perfectly!
  Thanks again James.
  Aubrey.
  This tutorial has been helpful, thank you very much!
  Hello,
  I am get the recorded audio.
  Is there any one could help me for to change the recorded voice in to mechanical voice ,i.e robot, cat.
  Thanks in advance
  Mukund
  Hi,
  I setup the code in Eclipse with Android 2.2 SDK. But I get error in Activity file:
  R cannot be resolved to a variable.. sorry im a noob..
  Thank you in advance.
  FlinxSYS
  You can regenerate the R.java file if you right click the project in eclipse, does that help?
  hi james thanks for the tutorial…I am doing a project on voice recognition i am new to android.My project is as follows as we say send msg to [recipient][message] it should send to the particular person i have mentioned in the same way to call, to listen to music, to view map,and to show directions through map can u please help me out while coding
  Hi Vijay,
  Interesting project you have going there, is it for university or just for fun?
  I’m not quite sure what you are trying to do, but it sounds as if you want to speak something and have it sent to someone. I’d suggest you have a read about voice recognition on android and cook up a few quick little apps to play around. If you get stuck with anything specific please let me know!
  just give me the overview of the project how to start
  hmmmm thank u so much james for ur quick response…i am just getting trained in android in a small institute…and just want to do my first project…i will send u the abstract of the project so that u can better understand…..
  Abstract for voice applications:
  Send text messages
  Say “send text to [recipient] [message]*”
  e.g. “send text to Vijay “I am busy call u around 9″
  Listen to music
  Say “listen to [artist/song/album]”
  e.g. “listen to the kolaveri”
  Call contacts
  Say “call [contact name] [phone type]*”
  e.g. “call Vijay home”
  Send email
  Say “send email to [recipient] [subject]* [body]*”
  e.g. “send email to Vijay How’s life in India treating you? The weather’s beautiful here!”
  View a map
  Say “map of [address/city]”
  e.g. “map of New Delhi”
  Go to websites
  Say “go to [website]
  e.g. “go to Wikipedia”
  Write a note(Remainder)
  Say “note to self [message]”
  e.g. “note to self grocery list banana milk eggs pizza”
  Search Google
  Say “[your query]”
  e.g. “pictures of the golden gate bridge at sunset”
  OK so your app needs to sit there listening, then when it hears one of a preset list of commands, determine which one it is, then action it accordingly, conceptually quite easy.
  Best thing I can suggest, is to break this down and experiment with some code. Create a basic app that will listen for commands, then repeat them back to you. Next, create an app that will action the commands when you click a button. Finally, scrap both of them and start fresh, using the knowledge you have, and tie them both up together.
  Voice recognition is quite nice, but it can sometimes struggle with regional accents, so bear that in mind when developing, speak like an American :)
  HMMMMM THANK U JAMES WILL TRY IT
  Thanks for your help earlier. If I have recognizer close and open again for next command, why do i get 1st results, and not 2nd? Do I have to close recognizer to send 2nd results to be stored in results? or?
  Thank you in advance.
  FlinxSYS
  HI james how to read the contact names through voice i am unable to do that in my project its not taking a noun
  Hi James, I found your interesting tutorial when searching VR Android. I’m working on a project to bring VR to our learning platform. You know many young children are shy or can’t speak correctly even when they are 3 or 4?
  The idea is to design a game that can recognize the input from a child player. For example the child can say “bring me an apple”, and game character will do accordingly. Do you think it’s realistic to use the current Android open source? Do you provide programming service? Thanks
  Dan
  Sure, anything is possible if you invest enough effort. Really depends how clear the child speaks, if they mumble a little bit I’d imagine the VR engine may struggle to detect what they’re saying
  hello, i want to make a project speech recognition on andriod.. please sugess me from where i have to start…. and what type of software i need…
  Hi James. Thanks for this. I understand the difference between ACTION_WEB_SEARCH and ACTION_RECOGNIZE_SPEECH as explained in Android dev docs and your example here..
  But I am wondering if you or anyone else has practical experience with getting qualitative differences in results using these 2 different language models. I’d like to use the voice recognition in a tutorial that uses voice to select from a finite set of button labels. stacked with this. I have combed the various forums over the net but can’t find the answer. I am using android 4.0.3. Also, will this googlevoice run in an emulator? thanks… stucked with this. I have already combed various forums over the net but can’t find the answer. I am using android 4.0.3. Also, will this googlevoice run in an emulator? thanks…
  Thank You for this brilliant tutorial. I have been coding for android for about 3 months and this tutorial can just help me to create a voice assistant to understand user commands!!
  Hi James, nice posting and will definitely try the tutorial u suggesting, btw can this tutorial be running in android 2.2 environment?
  Hi James, how can I contact you regarding an android application development?
  Hi James,
  Great tutorial. Simple to follow and great to get started with.
  I followed and it works fine but I’m working on an android project that requires to convert a mp3 record to text. Any idea how can I do this?
  Thanks
  June
  There is a message I have seen that always “voice recognizer not present”
  why ?
  what is the solution?
  That is because you have no VR present, download google voice from the market
  Hi Dude,
  I am trying to create a service which should always listen for any voice command(if not screen locked), I need your help in that…. Pls reply me as soon as possible…!
  A service class would probably handle that well, perhaps coupled with a widget would be nice :)
  hello,
  i just wanted to get the only word which i spoke , how to get that you app gives the list of all possible words
  reply ASAP
  THANK YOU
  hello,James.
  I’m magicFox.
  Have you know that Google service Voice recognition SDK or the jar file? I want to use the plug in the the application.If you have some idear,can you give me a email?mine:hyxand4503@163.com.MSN:magic_fox@msn.cn
  Thank you very much!
  Hey! James, do you have any idea how can i make recognition forever in loop (without button clicking). Thank you! Sorry for bad english (
  Hey james i currently working on a app that should detect a Beep and trigger an event in the database . Can you help me the code and sample code . I m thinking of using it as a service which will run in background and keeps waiting for the beep to occur and it will store the current time in the database . Can you please help out with this .I am new to android . My email-id -deepesh.shetty@spanservices.com .
  Thank you
  hi james,
  no doubt it is a great tutorial.. but i like to know can we will do voice recognition without internet….
  i will be very thankful to youy if you will provide me the code…for it…plz revert me…
  Hi James, I have a question. Do you know of a way to make the speech recognition listen for an extended period of time?
  Thanks
  It seems to be working but I got no results in the ListView. So I tried Google Search and it works fine.
  Please let me know what could be the problem.
  thanks for your tutorial!!
  im trying to build voice based media player for visually impaired people, could you please help by integrating the voice code with media player.
  hey james… thanks alot for a great tutorial…
  But I am trying to do some thing different by the help of this tutorial……
  I am doing voice recognition but without internet or you say offline. please help me in doing this. As i have done wioth this code it takes help of google api. So please help me in this that I will search or call my words but without the help of google api…. if possible plz help me in doing this