一旦我调用rec.start(),它就会立即调用onstart,然后立即调用onend,永远不会进入onresult。
网页允许使用麦克风,由于是 https Using Chrome 81.0.4044.122 on osx,它被询问过一次,它被列为支持语音识别。
我知道这在浏览器中不是一个稳定的功能,但运行 annyang 演示工作,检测我的声音,我相信这是使用相同的语音识别工具包。 https://www.talater.com/annyang/
有任何想法吗?
这是我的代码
if (!window.webkitSpeechRecognition) {
// not called, so assume browser supports webkitSpeechRecognition
alert('Your browser doesn\'t support speech to text.\nTry Chrome 33+ :)');
} else {
const promise = new Promise((resolve, reject) => {
const recognition = new webkitSpeechRecognition();
recognition.lang = lang;
recognition.continuous = true; // tried true and false
recognition.onstart = (e) => {
// called immediately after .start() as expected
console.log("onstart");
}
recognition.onend = () => {
// called immediately after onstart, not as expected
console.log("onend");
}
recognition.onresult = function(event) {
// never called, because it ended as soon as it started
console.log("onresult");
};
// also tried the following formats
// recognition.onresult = (event) => {};
// recognition.addEventListener('onresult', (event) => {};
// original example written like the following
// recognition.addEventListener('result', (event) => {};
recognition.start();
});
return await promise;
}