Google chrome 未在语音上触发 onResult 事件
Google chrome is not firing onResult event on voice
此代码中一切正常,除了在收到任何 audio.Can 后显示的结果部分,有人告诉我为什么即使在我用耳机说话后也没有在控制台中记录任何值。
<!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 中尝试此代码。稍后在控制台中 -> 搜索结果 -> 展开结果选项卡 -> 搜索名为 transcript 的内容。就是这样,你录制的文本。
<!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>
使用语音识别需要 Internet 连接,因为录制的语音必须通过 Internet 连接提供给 Web 服务进行处理。
https://developer.mozilla.org/en-US/docs/Web/API/Web_Speech_API/Using_the_Web_Speech_API
此代码中一切正常,除了在收到任何 audio.Can 后显示的结果部分,有人告诉我为什么即使在我用耳机说话后也没有在控制台中记录任何值。
<!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 中尝试此代码。稍后在控制台中 -> 搜索结果 -> 展开结果选项卡 -> 搜索名为 transcript 的内容。就是这样,你录制的文本。
<!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>
使用语音识别需要 Internet 连接,因为录制的语音必须通过 Internet 连接提供给 Web 服务进行处理。
https://developer.mozilla.org/en-US/docs/Web/API/Web_Speech_API/Using_the_Web_Speech_API