【问题标题】:Speech recognition plugin not working in Cordova 6语音识别插件在 Cordova 6 中不起作用
【发布时间】:2016-06-12 09:59:54
【问题描述】:

我正在使用 Apache Cordova 的 Visual Studio 工具来开发 Android 应用程序。我开始了新项目并使用 GIT url 添加了语音识别插件。

https://github.com/macdonst/SpeechRecognitionPlugin

安装成功,项目构建也成功。当我运行应用程序时,在下面的代码中,它在语音识别插件初始化之前显示一个警报,并且在下面的代码中永远不会达到第二个警报。

function onDeviceReady() {
        // Handle the Cordova pause and resume events
        alert('test');
        recognition = new SpeechRecognition();
        alert('test 2');
        recognition.onresult = function (event) {
            if (event.results.length > 0) {
                alert(event.results[0][0].transcript);
                q.value = event.results[0][0].transcript;
                //q.form.submit();
            }
        }
        alert('test 2');
        document.addEventListener( 'pause', onPause.bind( this ), false );
        document.addEventListener( 'resume', onResume.bind( this ), false );

        // TODO: Cordova has been loaded. Perform any initialization that requires Cordova here.
        var element = document.getElementById("deviceready");
        element.innerHTML = 'Device Ready';
        element.className += ' ready';
    };

请帮忙,我在添加插件时是否遗漏了什么?

【问题讨论】:

  • 控制台有错误吗?您在哪个平台上进行测试?
  • 你在“recognition”之前错过了一个“var”。该项目默认使用严格模式。在严格模式下,“var”是定义变量所必需的。

标签: javascript cordova cordova-plugins visual-studio-cordova


【解决方案1】:

被同样的问题困扰了很长时间。 终于可以破解了。诀窍是添加cordova media plugin

工作代码如下:

index.html

<!DOCTYPE html>
<html>
    <head>        
        <meta name="format-detection" content="telephone=no">
        <meta name="msapplication-tap-highlight" content="no">
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
        <link rel="stylesheet" type="text/css" href="css/index.css">
        <title>Speech Recognition</title>
    </head>
    <body>      
        <br>
        <br>
        <br>
        <br>
        <br>
        <br>
        <br>
        <br>
        <form>
        Click to speak <input type="button" value="speak" name="Download" id="speak" />  <br>
        <input type="text" id="q" name="q" size=60>
        </form>

        <script type="text/javascript" src="js/jquery.js"></script> 
        <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript" src="js/app.js"></script>
    </body>
</html>

app.js

$(document).ready(function() {
    document.addEventListener("deviceready", onDeviceReady, false);
});

var recognition;
function onDeviceReady() {  

    $('#speak').click( function() {
        recognition = new SpeechRecognition();          
        recognition.onresult = function(event) {
            if (event.results.length > 0) {
                console.log(event.results[0][0].transcript);                
                q.value = event.results[0][0].transcript;
            }
        };      
        recognition.start();
    });
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多