gpt4 book ai didi

javascript - 语音识别不适用于 chrome

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

我正在尝试使用下面的 javascript 代码从 chrome 浏览器访问音频,但是当我单击按钮时,recognition.onresult 事件没有在 chrome 浏览器中触发,但是当我访问在线时包含语音到文本的现场演示的网页,它在 chrome 浏览器中工作正常,请帮助。

<script type="text/javascript">
var recognition = new SpeechRecognition();
recognition.onresult = function(event)
{
if (event.results.length > 0)
{
alert("Working");
}
}
</script>

<form>
<input type="button" value="Click to Speak"onclick="recognition.start()">
</form>

最佳答案

我刚刚在我的电脑上试了一下,它可以工作:

$(function () {
try {
var recognition = new webkitSpeechRecognition();
} catch (e) {
var recognition = Object;
}
recognition.continuous = true;
recognition.interimResults = true;
recognition.onresult = function (event) {
var txtRec = '';
for (var i = event.resultIndex; i < event.results.length; ++i) {
txtRec += event.results[i][0].transcript;
}
$('#txtArea').val(txtRec);
};
$('#startRecognition').click(function () {
$('#txtArea').focus();
recognition.start();
});
$('#stopRecognition').click(function () {
recognition.stop();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button id="startRecognition">Start Recognition</button>
<button id="stopRecognition">Stop Recognition</button>
<textarea id="txtArea"></textarea>

关于javascript - 语音识别不适用于 chrome,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34097387/

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