是否需要在每次演讲后创建新的语音识别实例?
var recognition = new SpeechRecognition();
recognition.start();
或者只是停止()并再次调用start()func?
recognition.stop();
recognition.start();
只需要 1 个实例即可与语音识别对象交互。
您可以使用 start() 启动侦听器。 您可以使用 stop() 或 abort() 停止侦听器。
abort() 方法与 stop 方法略有不同:
Web 语音 API 的 abort() 方法停止语音识别 从收听传入音频的服务,并且不会尝试 返回语音识别结果。
这是直接来自文档的示例:
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.');
}
从语音识别文档中了解更多信息。
您可以使用以下命令暂停当前实例,然后继续当前实例:
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);
我已经测试过了,效果很好。