我需要能够设置将歌曲发送给中央JPlayer的链接。
例如,在商店中,有人想在购买之前对曲目进行采样,您可以单击该链接,然后将信息发送给JPlayer并开始播放所述曲目。
我已经看到在其他stackoverflow问题中使用类似于下面的一些例子here(7岁)然而无法让它工作。
更新:我尝试过以下但链接没有做任何事情。
$(document).ready(function(){
$("#jquery_jplayer").jPlayer({
swfPath: "../../dist/jplayer",
supplied: "mp3",
wmode: "window"
});
$('a[data-mp3]').click(function(e) {
e.preventDefault();
// replace #my-j-player by the id of your instance
$("#jquery_jplayer").jPlayer("setMedia", {
mp3: $(this).attr("data-mp3"),
oga: $(this).attr("data-ogg")
}).jPlayer("play");
});
});
<a href="#" data-mp3="http://www.jplayer.org/audio/mp3/Miaow-01-Tempered-song.mp3">TEST</a>
这是一个你可以使用的技巧。你必须让jQuery在所有具有data-mp3
属性的链接上的clickEvents上“监听”:
<div id='jquery_jplayer'></div> <!--wrapper for jPlayer-->
<a href="#" data-mp3="urlofmp3">
<a href="#" data-mp3="urlofmp3-2">
$(document).ready(() => {
// initialization of jPlayer
$("#jquery_jplayer").jPlayer({
swfPath: "https://cdnjs.cloudflare.com/ajax/libs/jplayer/2.9.2/jplayer/jquery.jplayer.swf",
supplied: "mp3",
});
// add event listener on ALL links that has a data-mp3 attribute
// and send the url to jPlayer then play
$('a[data-mp3]').click(function(e) {
e.preventDefault();
// replace #my-j-player by the id of your instance
$("#jquery_jplayer").jPlayer("setMedia", {
mp3: $(this).attr("data-mp3")
}).jPlayer("play");
});
});