【问题标题】:JS SpeechSynthesis Problems with the cancel() Methodcancel() 方法的 JS SpeechSynthesis 问题
【发布时间】:2016-09-08 12:53:00
【问题描述】:

我想在 Chrome 中使用 window.SpeechSynthesis 的 cancel 方法来切断一个话语并开始一个新的(这样你就不必听到所有仍在队列中的话语)

var test = new SpeechSynthesisUtterance("Test");
window.speechSynthesis.speak(test);  
window.speechSynthesis.cancel();
var test2 = new SpeechSynthesisUtterance("Test2");
window.speechSynthesis.speak(test2);

预期:使用 var test 开始讲话,但由于 cancel() 而立即取消它。 然后使用 var test2 再次开始语音,应该可以正常工作。

那当然没有发生。但发生的一切都没有。 :D 在 cancel() 之后调用 speak() 似乎什么也没做。

API 说明如下:

此方法从队列中删除所有话语。如果一个语句是 说话时,说话立即停止。这个方法不变 全局 SpeechSynthesis 实例的暂停状态。

谢谢回答:)

【问题讨论】:

    标签: javascript google-chrome speech-synthesis


    【解决方案1】:

    我刚刚遇到了同样的问题,cancel 后发出 speak 将导致没有话语被说出。

    我在 clear() 调用之后添加了一个小超时(250 毫秒),它似乎有效:

    var sayTimeout = null;
    
    function say(text) {
        if (speechSynthesis.speaking) {
            // SpeechSyn is currently speaking, cancel the current utterance(s)
            speechSynthesis.cancel();
    
            // Make sure we don't create more than one timeout...
            if (sayTimeout !== null)
                clearTimeout(sayTimeout);
    
            sayTimeout = setTimeout(function () { say(text); }, 250);
        }
        else {
            // Good to go
            var message = new SpeechSynthesisUtterance(text);
            message.lang = "en-US";
            speechSynthesis.speak(message);
        }
    }
    

    【讨论】:

      【解决方案2】:

      现在似乎可以使用您提供的代码。

      $(document).on("click", "#speak", function() {
      
        var test = new SpeechSynthesisUtterance("Test");
        window.speechSynthesis.speak(test);
        window.speechSynthesis.cancel();
        var test2 = new SpeechSynthesisUtterance("Test2");
        window.speechSynthesis.speak(test2);
      
      });
      <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <div id="speak">CLICK ME TO HEAR TEXT</div>

      【讨论】:

      • 已确认。我在 Chrome 76 / Windows 7 中听到“Test2”。
      【解决方案3】:
      function texto_a_voz(reproducir, idioma) {
      
         var synth = window.speechSynthesis;
         var voices = [];
         voices = synth.getVoices();
      
         var utterThis = new SpeechSynthesisUtterance(reproducir);
         utterThis.voice = voices[idioma];
         utterThis.pitch = pitch.value;
         utterThis.rate = rate.value;
      
         if (synth.speaking) {
             // SpeechSyn is currently speaking, cancel the current utterance(s)
             synth.cancel();
             setTimeout(function () { synth.speak(utterThis); }, 250);
         }
         else {
             // Good to go
             synth.speak(utterThis);
         }
      } 
      

      【讨论】:

        【解决方案4】:

        直接从 p5speech 库的示例中获取此代码。也许这有助于解决问题?

            function parseResult()
            {
                // recognition system will often append words into phrases.
                // so hack here is to only use the last word:
                var mostrecentword = myRec.resultString.split(' ').pop();
                if(mostrecentword.indexOf("left")!==-1) { dx=-1;dy=0; }
                else if(mostrecentword.indexOf("right")!==-1) { dx=1;dy=0; }
                else if(mostrecentword.indexOf("up")!==-1) { dx=0;dy=-1; }
                else if(mostrecentword.indexOf("down")!==-1) { dx=0;dy=1; }
                else if(mostrecentword.indexOf("clear")!==-1) { background(255); }
                console.log(mostrecentword);
            }
        

        【讨论】:

          猜你喜欢
          • 2014-12-08
          • 2015-07-06
          • 1970-01-01
          • 2014-09-25
          • 2021-02-04
          • 2020-08-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多