multiple mp3 links in a single page with jplayer

强颜欢笑 提交于 2019-12-11 08:55:29

问题


In my page there are a number of mp3 links and I use jplayer. How can I update jplayer function to play when clicked the seperate links?

$(document).ready(function() {

 $("#jquery_jplayer_1").jPlayer({

    ready: function(event) {
        $(this).jPlayer("setMedia", {
            mp3: "test_1.mp3",   //my mp3 file

        });
    },
    swfPath: "http://www.jplayer.org/2.1.0/js",
    supplied: "mp3"
  });
});    

my mp3 links :

<a href="javascript:listen(1);" class="jp-play" ></a>
<a href="javascript:listen(2);" class=" jp-play" ></a>
<a href="javascript:listen(3);" class=" jp-play" ></a>
<a href="javascript:listen(4);" class=" jp-play" ></a>
<a href="javascript:listen(5);" class=" jp-play" ></a>

回答1:


Hey you can do the following.

I instantiate the player on page load:

jQuery("#jquery_jplayer_1").jPlayer({
swfPath: "http://www.jplayer.org/latest/js/Jplayer.swf",
supplied: "mp3",
wmode: "window",
preload:"auto",
autoPlay: true,
errorAlerts:false,
warningAlerts:false
});

Then inside a listener, which will be unique for every item, you need to: A) Fetch track name/URL, I guess you should be able to figure this out.

B) Pass the track to the setMedia

jQuery("#jquery_jplayer_1").jPlayer("setMedia", {
mp3: "http:xxxx.rackcdn.com/"+track+".MP3"
});

C) Play the track

jQuery("#jquery_jplayer_1").jPlayer("play");

To get the track Id you need to install listeners on your playable items(maybe a a playable class?) and get the track from that one.




回答2:


html:

<a href="test_1.mp3" class="jp-play"></a>
<a href="test_2.mp3" class="jp-play"></a>

js:

$(document).ready(function() {
   readMP3("test_1.mp3");// play one mp3 if document is loaded

    $(".jp-play").click(function(event){
        event.preventDefault();
        readMP3($(this).attr("href"));
    })

});

function readMP3(_src){
 $("#jquery_jplayer_1").jPlayer({
    ready: function(event) {
        $(this).jPlayer("setMedia", {
            mp3: _src,
        });
    },
    swfPath: "http://www.jplayer.org/2.1.0/js",
    supplied: "mp3"
  });
}


来源:https://stackoverflow.com/questions/10471327/multiple-mp3-links-in-a-single-page-with-jplayer

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