Conversion of speech to text in Android devices using PhoneGap

Introduction:
Speech to text conversion is the mechanism to translate the spoken words into text. It is also known as automatic speech recognition. Whereas Android provides the native functionality to recognize the speech and convert the same to text. We just need to invoke the native module to fulfill our task.

Description:
While working with voice to text conversion in Android using PhoneGap I came across SpeechRecognizer plugin.It is very easy to integrate in PhoneGap application and also helps us to recognize the speech and convert the same to text in json array. So bellow are the steps to integrate this plugin with Android using PhoneGap.

1. Download the SpeechRecognizer plugin from github by using the below URL.

https://github.com/poiuytrez/SpeechRecognizer

2. Copy the SpeechRecognizer.java file and put it inside the src hierarchy of your project. In this case I put it inside src/com/phonegap/demo and renamed the package name in java file from com.phonegap.plugins.speech to com.phonegap.demo.

3. Add the plugin reference in res/xml/config.xml like this

	<plugin name="SpeechRecognizer" value="com.phonegap.demo.SpeechRecognizer">

4. Copy the SpeechRecognizer.js file and put it inside the www folder under assets.

5. Now create Index.html page and copy the following code inside it.



	
		<title>Speech To Voice Conversion</title>
		<script src="Phonegap/Cordova.js"></script>
		<script src="Jquery-1.9.1.min.js"></script>
		<script src="SpeechRecognizer.js"></script>
		<script src="Index.js"></script>
 	
	
		<input type="button" value="Speak Now" onclick=" RecognizeSpeech ();">
		<input type="button"  value=" Supported  Languages" onclick="GetSupportedLanguages();"/>;
       	

6. Create a JavaScript file named Index.js and copy the following codes into that file.

	function RecognizeSpeech () {
                	var maxMatches = 5;
                	var promptString = "Speak now"; 	// optional
               	 var language = "en-US";                     // optional
                	window.plugins.speechrecognizer.startRecognize(
	function(result){
                    			navigator.notification.alert (result);
                		}, function(errorMessage){
                    			console.log("Error message: " + errorMessage);
                		}, maxMatches, promptString, language);
            }

Here RecognizeSpeech function helps to recognize the speech and convert into text using startRecognize method.
We have to define the maxMatches which will return the maximum possible matches for the input speech. We can also pass optional parameter like promptString which will appear in the speech conversion box bellow the microphone icon and the language which will use by the speech recognition engine.

      	function GetSupportedLanguages() {
		window.plugins.speechrecognizer.getSupportedLanguages(
function(languages){
                    		// Display the  supported languages in json array
navigator.notification.alert (languages);
               	}, function(error){
navigator.notification.alert ("Could not retrieve the supported languages: " + error);
                	});
     	 }

The above function GetSupportedLanguages helps to find all the supported languages supported by the device using getSupportedLanguages method. It will return all the supported languages in json array.

Summary:
So using the above example we can easily recognize the speech and convert it to text using PhoneGap and the SpeechRecognizer plugin.Written By: Sourabha Kumar Sahoo, Software Developer, Mindfire Solutions

Advertisements

2 thoughts on “Conversion of speech to text in Android devices using PhoneGap

  1. Pingback: Conversion of speech to text in Android devices using PhoneGap | souravsahoo

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s