如果responsiveVoice isPlaying()则无法检测

时间:2015-01-11 09:28:16

标签: javascript jquery responsivevoice

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" 
/>
&#13;
&#13;
&#13;

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

5 个答案:

答案 0 :(得分:2)

他们实际上正在使用语音API的window.speechSynthesis对象。 因此,您可以检查其playing布尔值。

&#13;
&#13;
//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)
  })
&#13;
<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>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

感谢您使用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

答案 2 :(得分:0)

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

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

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

答案 3 :(得分:0)

isPlaying方法似乎并不存在。我能够使用responsiveVoice.OnFinishedPlaying钩子创建一个解决方案。这是一个你可以设置的功能,只要讲话完成就会被调用。这是一个小小的kludgy,但可以包装如下,以在文本完成时触发回调。当然,如果您在上一个文本完成之前尝试再次开始讲话,则会出现故障。

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

答案 4 :(得分:0)

&#34;音频播放完毕后有没有办法获得回电?&#34;

YES! 在查看了responsiveVoice代码之后,这个解决方案对我有用:

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

当:

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