【发布时间】:2018-08-14 16:16:44
【问题描述】:
我正在使用 Angular6 的 Web Speech API 包装器。我正在尝试在每 3.5 秒后实现一个启动-停止系统,以便能够操纵这些小部件的结果。
即使我停止识别,但在重新开始之前,我一直收到此错误Failed to execute 'start' on 'SpeechRecognition': recognition has already started。
按照这篇文章的建议,我首先验证语音识别是否处于活动状态,只有在不活动的情况下,我才会尝试启动它。 https://stackoverflow.com/a/44226843/6904971
代码如下:
constructor( private http: Http, private service: SpeechRecognitionService, private links: LinksService) {
var recognizing; // will get bool values to verify if recognition is active
this.service.onresult = (e) => {
this.message = e.results[0].item(0).transcript;
};
this.service.onstart = function () {
recognizing = true;
};
this.service.onaudiostart = function () {
recognizing = true;
};
this.service.onerror = function (event) {
recognizing = false;
};
this.service.onsoundstart = function () {
recognizing = true;
};
this.service.onsoundstart = function () {
recognizing = true;
};
this.record = () => {
this.service.start();
setInterval(root.ongoing_recording(), 3500);
};
var root = this;
var speech = '';
this.stop_recording = () => {
this.service.stop();
};
this.ongoing_recording = ()=> {
setTimeout(function(){
if( recognizing === true){
root.service.stop();
root.service.onend = (e) => {
recognizing = false;
speech = root.message;
var sentence = document.createElement('span');
sentence.innerHTML = speech + " ";
document.body.appendChild(sentence);
}
}
}, 3500);
setTimeout(function(){
if(recognizing === false){
root.service.start();
}
}, 3510);
};
}
start() {
this.service.start();
}
stop() {
this.service.stop();
}
record(){
this.record();
}
stop_recording(){
this.stop_recording();
}
ongoing_recording(){
this.ongoing_recording();
}
我认为时机可能不太好(使用 setTimeout 和间隔)。任何帮助将非常感激。谢谢! :)
【问题讨论】:
-
把 this.service.start();在尝试……抓住。然后,如果识别正在运行,您将捕获错误。
标签: javascript typescript speech-recognition angular6 webspeech-api