无法检测 responsiveVoice 是否正在播放()

Cant detect if responsiveVoice isPlaying()

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" 
/>

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

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

function isPlaying(audelem) { 
      return !audelem.paused; 
}

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

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

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

他们实际上是在使用 window.speechSynthesis 演讲的宾语 API。 所以你可以检查它的 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>

"is there a way to get callback once the audio is done playing?"

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

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

时间:

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

感谢使用 ResponsiveVoice!

自 v1.3.4 起,我们已将函数 isPlaying() 添加到 ResponsiveVoice 并更新了 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