语音识别插件在Cordova 6中不起作用 [英] Speech recognition plugin not working in Cordova 6
问题描述
我正在使用Visual Studio工具为Apache Cordova开发Android应用程序.我开始了新项目,并使用GIT网址添加了语音识别插件.
I am using Visual studio tools for Apache Cordova to develop Android app. I started new project and added speech recognition plugin using GIT url.
https://github.com/macdonst/SpeechRecognitionPlugin
安装成功,项目构建也成功.当我运行应用程序时,在下面的代码中,它将在语音识别插件初始化之前显示一个警报,而在下面的代码中则永远不会到达第二个警报.
It installed successfully and project build is also successful. When I run application, In below code, It shows one alert before Speech recognition plugin initialization and never reaches second alert after that in below code.
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';
};
请帮助,添加插件时我在这里缺少什么吗?
please help, am i missing something here while adding plug-in?
推荐答案
长期困扰同一问题. 终于能够破解了.诀窍是添加 cordova媒体插件.
Was stuck with the same issue for a longtime. Finally able to crack it. The trick was to add the 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();
});
}
这篇关于语音识别插件在Cordova 6中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!