简体   繁体   English

无法在“SpeechRecognition”上执行“start”:识别已经开始

[英]Failed to execute 'start' on 'SpeechRecognition': recognition has already started

I am using a wrapper of Web Speech API for Angular6.我正在为 Angular6 使用 Web Speech API 的包装器。 I am trying to implement a system of starting-stopping after each 3.5s in order to be able to manipulate the results for these small parts.我试图在每 3.5 秒后实现一个开始停止的系统,以便能够操纵这些小部件的结果。

Even though I stop the recognition, before starting it again, I keep getting this error Failed to execute 'start' on 'SpeechRecognition': recognition has already started .即使我停止了识别,在再次开始之前,我仍然收到此错误Failed to execute 'start' on 'SpeechRecognition': recognition has already started

As suggested in this post, I first verify whether the speech recognition is active or not and only if not active, I try to start it.正如这篇文章中所建议的,我首先验证语音识别是否处于活动状态,如果未处于活动状态,我会尝试启动它。 https://stackoverflow.com/a/44226843/6904971 https://stackoverflow.com/a/44226843/6904971

Here is the code:这是代码:

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();
  }

I think that the timing might not be good (with the setTimeout and interval).我认为时机可能不好(使用 setTimeout 和间隔)。 Any help would be much appreciated.任何帮助将非常感激。 Thank you!谢谢! :) :)

one observation: you run setInterval() every 3500 ms to invoke ongoing_recording() , but then use setTimeout() with 3500 ms again within ongoing_recording() .一个观察结果:您每 3500 毫秒运行一次setInterval()以调用ongoing_recording() ,但随后在ongoing_recording()再次使用setTimeout()和 3500 毫秒。

Besides that, maybe logging the error handler --where recognizing is also set to false -- could help finding a solution:除此之外,也许记录错误处理程序——其中recognizing也设置为false ——可以帮助找到解决方案:
in past versions of the SpeechRecognition implementation, not every error did actually stop the recognition (I don't know if that is still the case).SpeechRecognition实现的过去版本中,并非每个错误都确实停止了识别(我不知道是否仍然如此)。 So it might be the case, that recognizing is reset due to an error that did not actually stop the recognition;因此,可能是这种情况,由于错误并没有真正停止recognizing而重置识别; if this is really the cause of the error when restarting recognition, it could be just catched & ignored.如果这确实是重新启动识别时出现错误的原因,则可能会被捕获并忽略。

Also it might be worth trying to re-start the recognition in the onend handler (and onerror ).此外,可能值得尝试在onend处理程序(和onerror )中重新启动识别。

I am not sure what is the reason that is causing it in your code, but i had the same error and what caused it in my case was that I was calling start() twice in a row, so what fixed it was adding a variable to check if the recognition has started or stopped, so if it has started and I clicked it again it would return speach.stop() to avoid using start() again.我不确定在您的代码中导致它的原因是什么,但我遇到了同样的错误,在我的情况下导致它的原因是我连续两次调用 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();
  };
});

I used Web Speech API for voice search functionality in my site and I was facing a similar sort of situation.我在我的网站中使用了 Web Speech API 来实现语音搜索功能,我也面临着类似的情况。 It has one microphone icon which toggles the speech recognition on and off.它有一个麦克风图标,可以打开和关闭语音识别。 It was working fine in the normal on and off of the button that started speech recognition but was breaking only if you test it rigorously with a continuous button toggle.它在启动语音识别的按钮的正常打开和关闭中工作正常,但只有在您使用连续按钮切换进行严格测试时才会中断。

Solution: The thing that worked for me is:解决方案:对我有用的是:

try{
//line of code to start the speech recognition
}
catch{
//line of code to stop the speech recognition
}

So I wrapped the .start() method which was breaking the application in a try block and then added the catch block to stop it.所以我包装了 .start() 方法,它在 try 块中破坏了应用程序,然后添加了 catch 块来停止它。 And even if it comes across this problem, on the next button click to turn on the speech recognition, it works.即使遇到这个问题,在下一个按钮单击以打开语音识别时,它也能工作。 I hope you would be able to extract something from it.我希望你能从中提取一些东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法执行npm启动 - Failed to execute npm start Backbone.history已经启动 - Backbone.history has already been started 在已经开始后停止提交表单 - Stop form submission AFTER it has already been started 检查Backbone.history的方法已经开始了吗? - Way to check Backbone.history has already been started? Javascript 在滚动已经开始后停止在 touchmove 上滚动 - Javascript stop scrolling on touchmove after scrolling has already started NetworkError:无法在“XMLHttpRequest”上执行“发送”:无法加载“”:文档已分离 - NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load '': Document is already detached 未能在“IDBObjectStore”上执行“放置”:事务已完成 - Failed to execute 'put' on 'IDBObjectStore': The transaction has finished 无法在'FileReader'上执行'readAsDataURL':对象已经忙于读取Blob - Failed to execute 'readAsDataURL' on 'FileReader': the object is already busy reading Blobs 使用Chutzpah运行QUnit(TypeScript)测试,在已经启动的情况下在测试上下文之外提供“Called start()” - Running QUnit (TypeScript) tests with Chutzpah gives “Called start() outside of a test context while already started” 仅当另一个条件已经执行时才执行条件 - Execute a conditional only if another conditional has already been executed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM