是否需要在每次演讲后创建 SpeechRecognition 的新实例?
var recognition = new SpeechRecognition();
recognition.start();
或者只是 stop() 并再次调用 start() 函数?
recognition.stop();
recognition.start();
是否需要在每次演讲后创建 SpeechRecognition 的新实例?
var recognition = new SpeechRecognition();
recognition.start();
或者只是 stop() 并再次调用 start() 函数?
recognition.stop();
recognition.start();
与 SpeechRecognition 对象交互只需要 1 个实例。
您可以使用start()启动侦听器。您可以使用stop()或abort()停止侦听器。
abort() 方法与 stop 方法略有不同:
Web Speech API 的 abort() 方法停止语音识别服务侦听传入的音频,并且不尝试返回 SpeechRecognitionResult。
这是直接来自文档的示例:
var recognition = new SpeechRecognition();
var speechRecognitionList = new SpeechGrammarList();
speechRecognitionList.addFromString(grammar, 1);
recognition.grammars = speechRecognitionList;
var diagnostic = document.querySelector('.output');
var bg = document.querySelector('html');
document.body.onclick = function() {
recognition.start();
console.log('Ready to receive a color command.');
}
abortBtn.onclick = function() {
recognition.abort();
console.log('Speech recognition aborted.');
}
recognition.onspeechend = function() {
recognition.stop();
console.log('Speech recognition has stopped.');
}
从SpeechRecognition文档中了解更多信息。
您可以使用以下命令暂停然后继续当前实例:
recognition.abort(); //and then followed by:
recognition.start();
如果您想使用新配置重新开始,例如更改语言:
recognition.lang = 'id-ID'; //example to change the language
recognition.stop(); //stop recoginition
//try to give a bit delay and then start again with the same instance
setTimeout(function(){ recognition.start(); }, 400);
我已经测试过了,效果很好。