【问题标题】:Cant detect if responsiveVoice isPlaying()无法检测是否 responsiveVoice isPlaying()
【发布时间】:2015-01-11 09:28:15
【问题描述】:

http://responsivevoice.org 上关于isPlaying() 的信息不多。

这是我尝试过的,但不起作用。我没有收到console.log()

setInterval(function(){ // continuously check if the audio is being played
  if(responsiveVoice.isPlaying()){
    console.log('playing..');
  }, 
    100
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://responsivevoice.org/responsivevoice/responsivevoice.js"></script>

<textarea id="text" cols="45" rows="3">Hello, world!</textarea>
 
<input 
  onclick="responsiveVoice.speak($('#text').val(),'US English Female');" 
  type="button" 
  value="Play" 
/>

如何检测音频是否正在播放?另外,有没有办法在音频播放完毕后获得回调?

【问题讨论】:

    标签: javascript jquery responsivevoice


    【解决方案1】:

    他们实际上是在使用语音 API 的 window.speechSynthesis 对象。 所以你可以检查它的playing布尔值。

    //Their code
      var voicelist = responsiveVoice.getVoices();
    
      var vselect = $("#voiceselection");
    
      $.each(voicelist, function() {
        vselect.append($("<option />").val(this.name).text(this.name));
      });
    
    // Yours
      $('#isPlaying').on('click', function() {
        $('#r').text(window.speechSynthesis.speaking)
      })
    <script src="http://responsivevoice.org/responsivevoice/responsivevoice.js"></script>
    <script src="http://code.jquery.com/jquery-git2.js"></script>
    
    <textarea id="text" cols="45" rows="3">Hello world this is some text to say</textarea>
    
    <select id="voiceselection"></select>
    
    <input onclick="responsiveVoice.speak($('#text').val(),$('#voiceselection').val());" type="button" value="Play" />
    <br>
    <button id="isPlaying">Playing:</button>
    <p id="r">?</p>

    【讨论】:

      【解决方案2】:

      感谢您使用 ResponsiveVoice!

      我们从 v1.3.4 开始向 ResponsiveVoice 添加了函数 isPlaying() 并更新了 API 文档。

      您现在可以使用:

      responsiveVoice.isPlaying() 
      

      适用于原生 SpeechSynthesis 和后备音频 TTS。

      你可以在这里得到它:

      http://code.responsivevoice.org/develop/1.3/responsivevoice.js(指向 1.3.x 周期中的最新版本)

      也可以获取特定版本:

      http://code.responsivevoice.org/develop/1.3.4/responsivevoice.js

      【讨论】:

        【解决方案3】:

        Audio 标签有一个paused 属性。如果它没有暂停,那么它正在播放。所以:

        function isPlaying(audelem) { 
              return !audelem.paused; 
        }
        
        $('audio').on('play ended',function() { 
            setInterval(function(){
                if(isPlaying(this)){
                   console.log('playing...');
                }
            }, 100);
        });
        

        【讨论】:

        • 对不起,我不能让你的代码在这个小提琴中工作:jsfiddle.net/5n8ekvcq。我错过了什么?
        • 经过检查,似乎&lt;audio&gt;不在HTML中。
        【解决方案4】:

        isPlaying 方法似乎并不实际存在。我能够使用 responsiveVoice.OnFinishedPlaying 钩子创建一个解决方案。这是一个您可以设置的功能,只要讲话完成就会调用它。它有点笨拙,但可以如下包装以在文本完成时触发回调。当然,如果您尝试在前一个文本完成之前再次开始讲话,这就会失败。

        function speak(text, voice, callback) {
             if (callback) {
                  responsiveVoice.OnFinishedPlaying = function () {
                       responsiveVoice.OnFinishedPlaying = null;
                       callback();
                  };
             }                
             responsiveVoice.speak(text, voice);
        }
        

        【讨论】:

          【解决方案5】:

          “有没有办法在音频播放完毕后获得回调?”

          是的! 在查看 responsiveVoice 代码后,这是对我有用的解决方案:

          responsiveVoice.speak(text, voice, {"onend": function() { msgonend(text); }});
          

          何时:

          function msgonend(text) {
              console.log("'" + text + "' ended.");
          ...        } 
          };
          

          【讨论】:

            猜你喜欢
            • 2019-11-02
            • 1970-01-01
            • 2012-10-09
            • 2018-09-05
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多