Android Speech Recognition Without Dialog In A Custom Activity


Android Speech Recognition Without Dialog

A very interesting feature introduced in android is speech recognition. Many apps like Google Now, Chrome browser app, Google Maps app use speech recognition for their text input. Most of the apps use the standard easy way to get the voice inputs i.e. startActivityForResult by a RecognizerIntent. This would launch a dialog on top of the app listen to voice input and would return the text back to the app. Now in this tutorial Android Speech Recognition Without Dialog in a Custom Activity, I would show how to do the speech recognition in android without this dialog box.

Recently I was working on an app where I wanted voice to text input, but I did not wanted to show the Google’s pre made voice input dialog. Another way of doing this is by using the IME voice input, i.e the speech recognition from the keyboard. All you need to do is enable this option from the “Language & Input” device settings. But in Android Speech Recognition Without Dialog is possible by one more method, that is to implement the RecognitionListener and override all of its callback methods. This way, one would be able to recognize speech without going to the Google’s original speech recognition dialog.

A drawback of Android Speech Recognition Without Dialog in a Custom Activity is that, we would also have to override the onRmsChanged callback method if we need to show the voice visualization while recording. In this tutorial I have used the standard android ProgressBar to show the voice levels.

To start of with Android Speech Recognition Without Dialog tutorial lets first define the permissions in the android manifest:

To use the SpeechRecognizer class which provides the access to speech recognition service we need to declare the android.permission.RECORD_AUDIO permission. Next lets have a look at the layout xml file activity_main.xml.

The key elements in this layout file are a ToggleButton, ProgressBar, and a TextView. In this layout the TextView would be used to display the results, ToggleButton would be used to start and stop the SpeechRecognizer API. Lastly the ProgressBar would be used to display the speech RMS fluctuations. In the end your layout would look something like this one below.

Android Speech Recognition Without Dialog

Lets move on to some real code now, but before that let me give you a little overview of RecognitionListener, RecognizerIntent, and SpeechRecognizer in android. Also how they work together in Android Speech Recognition Without Dialog in a Custom Activity tutorial. In Android there are two ways of speech recognition:

1. Start an activity for result, with an Intent that has some parameters:

This would launch a dialog on top of your activity and would start capturing voice. When its done it returns the text back to your activity.

2. The second way is the one where we implement the RecognitionListener and implement all of its call back methods. This Android Speech Recognition Without Dialog tutorial focuses mainly on this second part. Another interesting feature about speech recognition is that since jellybean OS has bean released you can also do an offline voice recognition, all you need to do is download a language pack from settings.

Offline Speech Recognition In Android

  1. Goto “Language & input” in settings
  2. Tap on “Voice Search”
  3. Tap on “Offline speech recognition”
  4. Select and download the desired package

Now lets have a look at the main class VoiceRecognitionActivity.java:

The main purpose of implementing this Android RecognitionListener interface is to do the speech recognition without displaying the Android/Google’s speech recognition dialog. lets start with onCreate method, here SpeechRecognizer.createSpeechRecognizer(this); is used to initialize the SpeechRecognizer object. This Android SpeechRecognizer class is the one through which one can access the speech recognition service. This object is the main object in this implementation through which we start and stop the voice recognition process.

Another important thing which I skipped in the beginning was the display of speech visualization. Generally when working with default Google’s Voice Search dialog, while the voice is being captured a visualization is displayed around the mic button. But now that we are implementing our own activity where voice will be captured, we also need to implement a voice visualization. This is done through the onRmsChanged(float rmsdB) method. An interesting observation about this method is that rmsdB is always between the range of -2.~ to 10.0, this is not stated anywhere in the official documentation, its just my personal observation. Also when I searched the net found someone here saying this. Therefore in the onBeginningOfSpeech() method I have set the max value of progress bar to 10. In the standard flow of RecognitionListener whenever voice fluctuations are observed the rmsdB is changed, and onRmsChanged method is called. In this implementation I use this method to update the progress bar. I would like to state that I have used this progress bar in this Android Speech Recognition Without Dialog in a Custom Activity tutorial for the sake of simplicity, one can use any sort of complex graphs to show a visualization.

Next when the toggle button is changed SpeechRecognizer.startListening(recognizerIntent); and SpeechRecognizer.stopListening(); methods are called accordingly, with an appropriate RecognizerIntent. After this implementation your app would look something like this:

One of the most important methods to build the Android speech Recognition App Without Pop Up, is onResults(Bundle results) method. Here in this method speech recognition result is passed as an argument in the form of an ArrayList. After this one can process the result, as required. In my case I just displayed it in a TextView. Hope this Android Speech Recognition Without Dialog in a Custom Activity tutorial helped you, if it did, please share this with your friends on Google+, Facebook, and don’t forget to like our Facebook page for updates.

About Mohit Gupt

An android enthusiast, and an iPhone user with a keen interest in development of innovative applications.


Leave a comment

Your email address will not be published. Required fields are marked *

32 thoughts on “Android Speech Recognition Without Dialog In A Custom Activity