离子应用无法识别Android手机中的语音

时间:2016-05-11 09:27:18

标签: javascript android cordova ionic-framework speech-recognition

我想按照教程here设计一个能够听取语音命令的离子应用程序 但是,如果使用var recognition = new webkitSpeechRecognition(); //To Computer命令在我的计算机上进行测试,应用程序似乎会识别麦克风上的语音命令。但是看到这个post我将命令替换为

var recognition = new SpeechRecognition(); // To Device

但这似乎不适用于我的Android设备.. 有人在SpeechRecognitionPlugin遇到同样的问题吗? 请分享您的想法和评论......谢谢

2 个答案:

答案 0 :(得分:2)

超时解决方法对我不起作用。但是,我确实注意到,如果我在哔哔声后立刻说话,我会得到几乎100%的结果。如果没有找到任何单词,我会向用户添加提示。

以下是基于我的方法的示例:

var recording = false;
var spokenInput = '';

function startRecognition() {
    if (!recording) {

        recording = true;
        spokenInput = '';

        var recognition = new SpeechRecognition();

        recognition.onresult = function(event) {
            if (event.results.length > 0) {
                spokenInput = event.results[0][0].transcript;
            }
        };

        recognition.onend = function() {
            recording = false;
            if (spokenInput) {
               alert(spokenInput);
            } else {
                alert('For best results, try speaking immediately after the beep!');
            }
        };

        setTimeout(function() {
            recognition.stop();
        }, 6000); // Force stop  after 6 seconds

        recognition.start();

    }
}

答案 1 :(得分: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();
    });
}