使用Phonegap在Android上启动语音识别器

使用Phonegap在Android上启动语音识别器

本文介绍了使用Phonegap在Android上启动语音识别器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在制作一个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版本不对。如果你使用新的cordova,你还必须使用最新版本的插件。此版本需要SDK 15或更高版本。 (android manifest - > < uses-sdk android:minSdkVersion =15android:targetSdkVersion =17/> )。
之后,由于某种原因插件init不返回任何东西。
我只是触发: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):

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'是您必须按下才能启动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在Android上启动语音识别器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-27 18:51