Play song at specific time position from a playlist

不想你离开。 提交于 2021-01-28 02:16:23

问题


I am using jPlayer plugin.

In example there are 13 songs in playlist. I want to set a start time (currentTime) for each song like : 1st song starts from 2 sec, 2nd song start from 3 sec and 5th song start from 4 sec.

Here is an example jsFiddle.

In this example I added var tag = $('.audio-tag audio')[0]; to find current playing song globally but it is out of jPlayer plugin.

How to get current audio tag & number to set currentTime for each song of jPlayer playlist?


回答1:


SOLUTION

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);
});  

NOTES

Call setTimeoout is needed because according to the 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.

DEMO

See this jsFiddle for code and demonstration.



来源:https://stackoverflow.com/questions/31564753/play-song-at-specific-time-position-from-a-playlist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!