使用PhoneGap的Andr​​oid的启动语音识别 [英] Start speech recognizer on Android using Phonegap

查看:264
本文介绍了使用PhoneGap的Andr​​oid的启动语音识别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在做的PhoneGap应用程序。
我想增强现实EN语音输入相结合。
没有为PhoneGap的所谓SpeechRecognizer一个插件,但我无法得到它的工作。

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.

我的头:

<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>

项目(config.xml)中的图片:

A picture of the project (config.xml):

在此先感谢

推荐答案

有几个问题。
首先,SDK版本是不正确的。如果使用新的科尔多瓦你也必须使用该插件的最新版本。此版本需要SDK 15或更高。 (Android清单 - > &LT;采用-SDK安卓的minSdkVersion =15机器人:targetSdkVersion =17/&GT; )。
在此之后,由于某种原因,插件init不会返回任何东西。
我只是triggerd的:window.plugins.speechrecognizer.startRecognize();一个按钮的功能单击,它执行。

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.

JavaScript的(你需要的jQuery为此code):

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'是你必须preSS按钮启动Android语音识别

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

#searchField'是一个输入字段至极从语音识别获得的结果

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

由于MrBillau了很好的建议。

Thanks to MrBillau for the good advice.

这篇关于使用PhoneGap的Andr​​oid的启动语音识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆