【问题标题】:Failed to execute 'start' on 'SpeechRecognition': recognition has already started无法在“SpeechRecognition”上执行“开始”:识别已经开始
【发布时间】: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


【解决方案1】:

一个观察: 您每 3500 毫秒运行一次 setInterval() 以调用 ongoing_recording(),然后在 ongoing_recording() 内再次使用 3500 毫秒的 setTimeout()

除此之外,也许记录错误处理程序——其中recognizing 也设置为false——可能有助于找到解决方案:
SpeechRecognition 实现的过去版本中,并非每个错误都确实停止了识别(我不知道是否仍然如此)。 所以可能是这样的情况,recognizing 被重置是因为错误并没有真正停止识别;如果这确实是重新启动识别时出错的原因,则可以将其捕获并忽略。

还可能值得尝试在onend 处理程序(和onerror)中重新开始识别。

【讨论】:

    【解决方案2】:

    我不确定在您的代码中导致它的原因是什么,但我遇到了同样的错误,在我的情况下导致它的原因是我连续两次调用 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();
      };
    });
    

    【讨论】:

      【解决方案3】:

      我在我的网站中使用 Web Speech API 来实现语音搜索功能,我遇到了类似的情况。它有一个麦克风图标,可以打开和关闭语音识别。它在启动语音识别的按钮的正常打开和关闭时工作正常,但只有在您使用连续按钮切换严格测试时才会中断。

      解决方案: 对我有用的是:

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

      所以我将 .start() 方法包装在 try 块中,该方法破坏了应用程序,然后添加了 catch 块来停止它。即使遇到这个问题,在下一个按钮上单击以打开语音识别,它也可以工作。我希望你能从中提取一些东西。

      【讨论】:

        猜你喜欢
        • 2023-03-23
        • 2013-04-06
        • 1970-01-01
        • 1970-01-01
        • 2013-08-06
        • 1970-01-01
        • 2017-09-03
        • 1970-01-01
        • 2019-08-02
        相关资源
        最近更新 更多