Javascript 语音识别在听后提供未知错误
我有一个Javascript 语音识别在听后提供未知错误,javascript,speech-recognition,Javascript,Speech Recognition,我有一个templates/index.html文件。当您单击第一个按钮时,btn\u query,您应该调用startDictation(),这是一个用于语音识别的javascript函数。但是有一个问题,它只出现在StackOverflow和Chrome上,recognition.onresult=function(e){…} 一,。我没有得到弹出的window.alert(5+6)在函数的开头 二,。StackOverflow控制台识别出有错误并写入:识别有错误。但我不知道是哪一个 它的行
templates/index.html
文件。当您单击第一个按钮时,btn\u query
,您应该调用startDictation()
,这是一个用于语音识别的javascript函数。但是有一个问题,它只出现在StackOverflow和Chrome上,recognition.onresult=function(e){…}
一,。我没有得到弹出的window.alert(5+6)代码>在函数的开头
二,。StackOverflow控制台识别出有错误并写入:识别有错误
。但我不知道是哪一个
它的行为是这样的:它请求使用麦克风的许可,然后顶部的标签上出现红灯,最后出现错误消息
<!DOCTYPE html>
<html style="margin: auto; display:table;">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript">
</script>
<script>var synth = window.speechSynthesis;</script>
<!-- HTML5 Speech Recognition API -->
<script>
function startDictation() {
document.getElementById('transcript').value = '';
document.getElementById('output').value = '';
if (window.hasOwnProperty('webkitSpeechRecognition')) {
var recognition = new webkitSpeechRecognition();
recognition.continuous = false;
recognition.interimResults = false;
recognition.lang = "en-US";
recognition.start();
recognition.onresult = function (e) {
window.alert(5 + 6);
document.getElementById('loader').hidden = false;
document.getElementById('transcript').value = e.results[0][0].transcript;
recognition.stop();
var data = e.results[0][0].transcript;
$.post("http://localhost:5000/news_urls", { "data": data },
function (response) {
document.getElementById('loader').hidden = true;
data = response;
document.getElementById("output").value = data["urls"];
}).error(function (response) {
document.getElementById('loader').hidden = true;
if (response.status == 400)
text = jQuery.parseJSON(response.responseText)["original_exception"];
else
text = "I'm sorry. I did not get that.";
document.getElementById("output").value = text;
});
};
recognition.onerror = function (e) {
recognition.stop();
console.log("Recognition had an error");
window.alert(10 + 6);
}
}
}
function btnClick() {
synth.cancel();
var utterThis = new SpeechSynthesisUtterance(document.getElementById("output").value);
utterThis.voice = synth.getVoices()[0];
utterThis.pitch = 1.0;
utterThis.rate = 0.8;
utterThis.onerror = function(e) { console.log("Something went wrong with utterance."); };
synth.speak(utterThis);
}
</script>
<style>
.speech {
border: 0px solid #DDD;
width: 600px;
padding: 0;
margin: 0;
font-family: "Calibri";
}
.speech input {
border: 1;
width: 240px;
display: inline-block;
height: 30px;
}
.speech img {
float: right;
width: 40px;
}
</style>
</head>
<body bgcolor="#e2e2e2">
<h1 style="font-family: Calibri;">Delbot</h1>
<div class="speech" ><i>It understands your voice commands, searches news and knowledge sources, and summarizes and reads out content to you.</i></div>
<br /><i class="speech"><font color="gray">Only tested on Windows PCs. Not tested on other PCs or mobile devices.</font></i>
<div class="speech">
<textarea style="width: 600px;font-family: Calibri;font-size:x-large" name="q" id="transcript"
placeholder="Your query will appear here after you speak." rows="2" readonly="True"></textarea>
<br>
<input id="btn_query" type="button" onclick="startDictation()" value="Query"
style="font-family: Calibri;" />
<img src="static/loader.gif" width="100px" align="left" style="float: left" hidden="True" id="loader" />
<br><br>
<h2 class="speech">Results</h2>
<textarea style="width: 600px;font-family: Calibri;font-size:x-large" id="output" rows="2" placeholder="Results will appear here."
readonly="True"></textarea>
<input id="btn_speak" type="button" value="Speak" onclick="btnClick()" style="font-family: Calibri;" />
</div>
</body>
</html>
var synth=window.speechSynthesis;
函数startDictation(){
document.getElementById('transcript')。值=“”;
document.getElementById('output')。value='';
if(window.hasOwnProperty('webkitSpeechRecognition')){
var recognition=new-webkitSpeechRecognition();
识别持续=错误;
recognition.interimResults=false;
recognition.lang=“en-US”;
识别。开始();
recognition.onresult=函数(e){
窗口警报(5+6);
document.getElementById('loader')。hidden=false;
document.getElementById('transcript')。value=e.results[0][0]。transcript;
识别。停止();
var数据=e.results[0][0]。转录本;
$.post(”http://localhost:5000/news_urls“,{data:data},
功能(响应){
document.getElementById('loader')。hidden=true;
数据=响应;
document.getElementById(“输出”).value=data[“URL”];
}).错误(功能(响应){
document.getElementById('loader')。hidden=true;
如果(response.status==400)
text=jQuery.parseJSON(response.responseText)[“原始异常”];
其他的
text=“对不起,我没听清楚。”;
document.getElementById(“输出”).value=text;
});
};
recognition.onerror=函数(e){
识别。停止();
log(“识别有错误”);
窗口警报(10+6);
}
}
}
函数btnClick(){
synth.cancel();
var utterThis=newspeechsynthesisutterance(document.getElementById(“output”).value);
utthis.voice=synth.getVoices()[0];
utthis.pitch=1.0;
这一比率=0.8;
utterThis.onerror=函数(e){console.log(“话语出了问题。”);};
合成说话(说这个);
}
.演讲{
边框:0px实心#DDD;
宽度:600px;
填充:0;
保证金:0;
字体系列:“Calibri”;
}
.语音输入{
边界:1;
宽度:240px;
显示:内联块;
高度:30px;
}
1.语音图像{
浮动:对;
宽度:40px;
}
德尔博特
它了解您的语音命令,搜索新闻和知识来源,并向您总结和读出内容。
仅在Windows PC上测试。未在其他PC或移动设备上测试。
结果
您似乎正在使用本地文件路径运行浏览器
例如,file://xyz/ss/ss/test.html
尝试在服务器上托管此HTML文件,并检查是否会得到适当的结果
检查并确认。因为chrome不支持允许麦克风具有本地文件路径。您可以强制执行,但不建议这样做。使用真正危险的--禁用web安全性(强烈不推荐,尤其是如果您将此Chrome实例用于正常浏览,这可能会使您的设备处于危险中)和--允许从文件访问文件(也不推荐)
调试:
通过访问本地文件,我得到了相同的错误。
但是在托管index.html
文件并访问http://localhost:1111
它工作得很好
注意:-一些编辑器将普通HTML加载到浏览器中。尝试下面的编辑器,它们可以承载您的文件并完美运行。只需将代码C+V放到编辑器中并运行即可。试试下面的编辑器
更新=============
是的!我的麦克风还没有安装好,似乎就到此为止。你知道为什么我没有结果吗?但这可能是另一个问题。所以,您是否已将文件托管在服务器上?或者您仍然在浏览器中直接访问HTML文件?我将其托管在本地主机服务器上。请看,您说话后应该会收到警报11
<代码>我没有结果
我不明白你的意思。您的API不返回结果吗?