1

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

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

4

5 回答 5

2

他们实际上是在使用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>

于 2015-03-06T02:48:41.603 回答
1

感谢您使用响应式语音!

我们从 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

于 2015-06-12T15:58:30.533 回答
0

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

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

$('audio').on('play ended',function() { 
    setInterval(function(){
        if(isPlaying(this)){
           console.log('playing...');
        }
    }, 100);
});
于 2015-01-11T09:53:25.543 回答
0

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

function speak(text, voice, callback) {
     if (callback) {
          responsiveVoice.OnFinishedPlaying = function () {
               responsiveVoice.OnFinishedPlaying = null;
               callback();
          };
     }                
     responsiveVoice.speak(text, voice);
}
于 2015-03-06T01:35:14.700 回答
0

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

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

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

什么时候:

function msgonend(text) {
    console.log("'" + text + "' ended.");
...        } 
};
于 2015-05-29T08:03:57.100 回答