Ionic应用程序无法识别android手机中的语音

Ionic app not recognizing speech in android mobile

本文关键字:手机 语音 android 识别 应用程序 Ionic      更新时间:2023-09-26

我想按照这里的教程设计一个可以监听语音命令的ionic应用程序但是,如果在我的电脑上使用var recognition = new webkitSpeechRecognition(); //To Computer命令进行测试,该应用程序似乎可以识别麦克风上的语音命令。但看到这篇帖子后,我将命令替换为

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

但这似乎在我的安卓设备上不起作用。。有人在SpeechRecognitionPlugin上遇到过同样的问题吗?请分享您的想法和评论。。。感谢

超时解决方法对我不起作用。然而,我确实注意到,如果我在嘟嘟声后立即说话,我会得到几乎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();
    }
}

终于破解了它。诀窍是添加了cordova媒体插件。

工作代码如下:

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();
    });
}