谷歌浏览器没有在语音上触发结果事件



除了收到任何音频后显示部分的结果外,此代码中的一切都运行良好。有人可以告诉我为什么即使在我戴着耳机说话后,这也不会在控制台中记录任何值。

<!DOCTYPE html>
<head></head>
<body>
<div id="msg"></div>
<div id="msg2"></div>
<script>
(function(){
var reco = new webkitSpeechRecognition();
var msgid = document.getElementById('msg');
reco.interimResults = true;
reco.addEventListener('start',function(){
msgid.innerHTML = 'Listening...';
});
reco.addEventListener('audiostart',function(){
msgid.innerHTML = 'Recording...';
});
reco.start();
reco.onresult = function(e){
console.log(e);
}
})()
</script>
</body>

但是,SpeechRecognition(( 是一个构造函数,大多数浏览器都不支持它,如果你在 Firefox 中运行它,它不会有帮助。在 chrome 中尝试此代码。稍后在控制台中 -> 搜索结果 -> 展开结果选项卡 -> 搜索称为成绩单的内容。就是这样,您录制的文本。

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
</head>
<body>
<button class="talk">Talk</button>
<h3 class="content"></h3>
<script>
const btn = document.querySelector(".talk");
const content = document.querySelector(".content");
const SpeechRecognition =
window.SpeechRecognition || window.webkitSpeechRecognition;
const myRecognition = new SpeechRecognition();
myRecognition.onstart = function() {
console.log("voice is activated, you may now speak");
};
myRecognition.onresult = function() {
console.log(event);
};
//adding eventListener
btn.addEventListener("click", () => {
myRecognition.start();
});
</script>
</body>
</html>

使用语音识别需要互联网连接,因为录制的语音必须通过互联网连接馈送到 Web 服务进行处理。

https://developer.mozilla.org/en-US/docs/Web/API/Web_Speech_API/Using_the_Web_Speech_API

最新更新