我正在使用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和interval)。任何帮助都将不胜感激。谢谢!:)
发布于 2018-10-06 02:06:55
一个观察结果是:您每3500毫秒运行一次setInterval()
来调用ongoing_recording()
,但是在ongoing_recording()
中再次使用3500毫秒的setTimeout()
。
除此之外,记录错误处理程序--其中recognizing
也设置为false
--可能有助于找到解决方案:
在以前版本的SpeechRecognition
实现中,并不是每个错误都会真正停止识别(我不知道现在是不是这样)。因此,可能的情况是,由于实际上没有停止识别的错误而重置了recognizing
;如果这确实是重新启动识别时错误的原因,则可以将其捕获并忽略。
此外,可能值得尝试在onend
处理程序(和onerror
)中重新启动识别。
发布于 2021-03-10 23:01:27
我不确定在您的代码中导致它的原因是什么,但是我有相同的错误,在我的例子中导致它的原因是我连续调用了start()两次,所以修复它的方法是添加一个变量来检查识别是否已经开始或停止,所以如果它已经启动并再次单击它,它将返回speach.stop()以避免再次使用start()。
let recognition = new SpeechRecognition();
let status = 0;
document.querySelector(".mic").addEventListener("click",() => {
if (status == 1) {
status = 0;
return recognition.stop();
}
recognition.start();
status = 1;
recognition.onresult = function (event) {
status=0;
var text = event.results[0][0].transcript;
recognition.stop();
};
recognition.onspeechend = function () {
status = 0;
recognition.stop();
};
});
发布于 2021-08-18 05:13:21
我在我的站点中使用Web Speech API来实现语音搜索功能,我也遇到了类似的情况。它有一个麦克风图标,可以打开和关闭语音识别。它在正常开启和关闭启动语音识别的按钮时工作正常,但只有当您使用连续的按钮切换进行严格测试时,它才会崩溃。
解决方案:对我有效的方法是:
try{
//line of code to start the speech recognition
}
catch{
//line of code to stop the speech recognition
}
因此,我包装了.start()方法,它在一个try块中破坏了应用程序,然后添加了catch块来停止它。即使它遇到了这个问题,单击下一步按钮打开语音识别,它也可以工作。我希望你能从中提取一些东西。
https://stackoverflow.com/questions/51845769
复制相似问题