在播放列表的特定时间位置播放歌曲

Play song at specific time position from a playlist

我正在使用 jPlayer 插件。

例如,播放列表中有 13 首歌曲。我想为每首歌曲设置一个开始时间 (currentTime),例如:第一首歌曲从 2 秒开始,第二首歌曲从 3 秒开始,第五首歌曲从 4 秒开始。

这是一个例子jsFiddle

在此示例中,我添加了 var tag = $('.audio-tag audio')[0]; 以全局查找当前正在播放的歌曲,但它不在 jPlayer 插件中。

如何获取当前音频 tag & number 为 jPlayer 播放列表的每首歌曲设置 currentTime?

解决方案

var $jp = $('#jquery_jplayer_1');
$jp.on($.jPlayer.event.setmedia,  function(e){
   console.log("Current track", e.jPlayer.status.media);
   console.log("Currentr track index", myPlayer.current);

   // For first track (0) - 2 sec, second track (1) - 3 sec, etc.
   var time = myPlayer.current + 2;

   // Jump to desired time
   setTimeout(function(){ 
       $jp.jPlayer( "play", time); 
   }, 100);
});  

注释

需要调用 setTimeoout 因为根据 manual

If issued immediately after a setMedia command, with the time parameter, and when the browser is using the HTML5 solution, this command will initially fail and an internal timeout is setup to retry the command every 100ms until it succeeds.

演示

有关代码和演示,请参阅 this jsFiddle