无法对“语音识别”执行“开始”:识别已经开始

时间:2018-08-14 16:16:45

标签: javascript typescript speech-recognition angular6 webspeech-api

我正在为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)。任何帮助将非常感激。谢谢! :)

2 个答案:

答案 0 :(得分:0)

一个观察: 您每3500毫秒运行setInterval()来调用ongoing_recording(),但是随后在setTimeout()内再次使用3500毫秒使用ongoing_recording()

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

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

答案 1 :(得分:0)

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