从onclick事件中播放JPlayer上的歌曲

问题描述 投票:-1回答:1

我需要能够设置将歌曲发送给中央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>
javascript jplayer
1个回答
0
投票

这是一个你可以使用的技巧。你必须让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");
    });
});

请检查这个minimal working example

© www.soinside.com 2019 - 2024. All rights reserved.