Start speech recognizer on Android using Phonegap

2019-04-11 23:21发布

问题:

Currently I'm making a Phonegap application. I want to combine augmented reality en speech input. There is a plugin for Phonegap called SpeechRecognizer, But I can't get it to work.

My header:

<script type="text/javascript" src="cordova-2.6.0.js"></script>
    <script type="text/javascript" src="SpeechRecognizer.js"></script>
    <script type="text/javascript" charset="utf-8">
        document.addEventListener("deviceready", onDeviceReady, false);

        function speechOk() {
            alert('speech works');
        }

        function speechFail() {
            alert("speech doesn't work");
        }

        function onDeviceReady() {
            window.plugins.speechrecognizer.init(speechOk, speechFail);
        }

        $("#micButton").bind("touchstart", function() {     
            var requestCode = 4815162342;
            var maxMatches = 1;
            var promptString = "What do you want?";
            window.plugins.speechrecognizer.startRecognize(speechOk, speechFail, requestCode, maxMatches, promptString);
        });
    </script>

A picture of the project (config.xml):

Thanks in advance

回答1:

Is not your fault, the SpeechRecognizer.java has a bug inside.

I had the same problem and I solved it with just replacing the Speech Recognizer plugin with and older version (like 2.0.0), you can download it from github.

It worked for me with Phonegap 2.5.0, guess it works in 2.6.0.



回答2:

There were a few problems. First of all, the SDK version wasn't right. If you use the new cordova you also have to use the newest version of the plugin. This version requires SDK 15 or higher. (android manifest -> <uses-sdk android:minSdkVersion="15" android:targetSdkVersion="17" />). After that, for some reason the plugin init does not return anything. I just triggerd the: window.plugins.speechrecognizer.startRecognize(); function on a button click, and it executes.

The javascript (you need jQuery for this code):

    $("#micButton").bind("touchstart", function() {        
        var requestCode = 4815162342;
        var maxMatches = 1;
        var promptString = "What do you want?";
        window.plugins.speechrecognizer.startRecognize(speechOk, speechFail, requestCode, maxMatches, promptString);
    });

    function speechOk(result) {
        var match, respObj;
        if (result) {
            respObj = JSON.parse(result);
            if (respObj) {
                var response = respObj.speechMatches.speechMatch[0];
                $("#searchField").val(response);
                $("#searchButton").trigger("touchstart");
            } 
        }
    }

    function speechFail(m) {
        navigator.notification.alert("Sorry, I couldn't recognize you.", function() {}, "Speech Fail");
    }

'#micButton' is the button you have to press to start the android voice recognition

'#searchField' is a input field wich gets the result from the voice recognition

Thanks to MrBillau for the good advice.