簡體   English   中英

無法在“SpeechRecognition”上執行“start”:識別已經開始

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

我正在為 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 和間隔)。 任何幫助將非常感激。 謝謝! :)

一個觀察結果:您每 3500 毫秒運行一次setInterval()以調用ongoing_recording() ,但隨后在ongoing_recording()再次使用setTimeout()和 3500 毫秒。

除此之外,也許記錄錯誤處理程序——其中recognizing也設置為false ——可以幫助找到解決方案:
SpeechRecognition實現的過去版本中,並非每個錯誤都確實停止了識別(我不知道是否仍然如此)。 因此,可能是這種情況,由於錯誤並沒有真正停止recognizing而重置識別; 如果這確實是重新啟動識別時出現錯誤的原因,則可能會被捕獲並忽略。

此外,可能值得嘗試在onend處理程序(和onerror )中重新啟動識別。

我不確定在您的代碼中導致它的原因是什么,但我遇到了同樣的錯誤,在我的情況下導致它的原因是我連續兩次調用 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();
  };
});

我在我的網站中使用了 Web Speech API 來實現語音搜索功能,我也面臨着類似的情況。 它有一個麥克風圖標,可以打開和關閉語音識別。 它在啟動語音識別的按鈕的正常打開和關閉中工作正常,但只有在您使用連續按鈕切換進行嚴格測試時才會中斷。

解決方案:對我有用的是:

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

所以我包裝了 .start() 方法,它在 try 塊中破壞了應用程序,然后添加了 catch 塊來停止它。 即使遇到這個問題,在下一個按鈕單擊以打開語音識別時,它也能工作。 我希望你能從中提取一些東西。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM