function onDeviceReady() { window.plugins.speechrecognizer.init(onConversionSucess, onConversionFail) } function onConversionSucess(){ var requestCode = 12345; var maxMatches = 1; // you can increase the number. var promptString = "Speak Now"; // you can change the prompt message. window.plugins.speechrecognizer.startRecognize(onSuccess, onError, requestCode, maxMatches, promptString); } function onConversionFail(){ navigator.notification.alert("Could not convert the speech to text:" + error); } function onSuccess(data) { if (data) { var jsonObject = JSON.parse(data); // It will return the first matched text for the speech. var convertedText = jsonObject.speechMatches.speechMatch[0]; $("#results").html(convertedText); } } function onError(error) { navigator.notification.alert("Conversion failed due to: " + error); } 6. Now create a html page and copy the follwing code inside the page. <!DOCTYPE HTML> <html> <head> <title>Speech To Voice Conversion</title> <script src="cordova-2.0.0.js"></script> <script src="jquery-1.9.1.min.js"></script> <script src="SpeechRecognizer.js"></script> <script src="index.js"></script> </head> <body> <input type="button" id="speechButton" value="Speak Now" onclick="onDeviceReady();"/> <div id="results"></div> </body> </html>
Now you are ready to convert the speech to text by clicking the “Speak Now” button.