gpt4 book ai didi

javascript - 语音识别在收听后提供未知错误

转载 作者:行者123 更新时间:2023-11-30 20:20:26 25 4
gpt4 key购买 nike

我有一个 templates/index.html 文件。当您单击第一个按钮 btn_query 时,您应该调用 startDictation(),这是一个用于语音识别的 javascript 函数。但是有一个问题,它只出现在 StackOverflow 和 Chrome 上,带有 recognition.onresult = function (e) {...}

1. 我没有在函数开头弹出 window.alert (5 + 6);

2. StackOverflow 控制台识别出错误并写入:Recognition had an error。但我不知道是哪一个。

它的行为是这样的:它请求使用麦克风的权限,然后顶部的选项卡上有一个红灯,最后是错误消息。

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code><!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></code></pre>
</div>
</div>

enter image description here

最佳答案

您似乎正在使用本地文件路径进入浏览器。

For e.g. file://xyz/ss/ss/test.html

尝试在服务器上托管此 HTML 文件并检查您将获得适当的结果。

Check this and this. As chrome does not support to allow microphone with a local file path. You can forcefully do it but it is not recommended. With the really dangerous --disable-web-security (strongly not recommended, especially if you use this instance of Chrome for normal browsing as well, which can put your device in danger) and --allow-file-access-from-files (also not recommended).

调试:

访问本地文件时出现同样的错误。但是在托管 index.html 文件并访问 http://localhost:1111 时,它运行良好。

enter image description here

注意:-一些编辑器将纯 HTML 加载到浏览器中。试试下面的编辑器,它们托管您的文件并完美运行。只需将您的代码 C+V 到编辑器并运行即可。尝试使用以下编辑器。

  1. > https://www.tutorialspoint.com/online_javascript_editor.php
  2. > https://www.w3schools.com/tryit/tryit.asp?filename=tryhtml_default

更新=============

enter image description here

关于javascript - 语音识别在收听后提供未知错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51504462/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com