Search code examples
jqueryjplayer

jPlayer text link does not play


i am trying to do a basic jplayer text link on click to play an mp3 file, but i am unable to get it to function as there is no sound. here is the code

$(document).ready(function(){
$("#jquery_jplayer").jPlayer({
    ready: function (event) {
        $('.voice').click(function(e) {
            e.preventDefault();
            $(this).jPlayer("setFile", $(this).attr('href')).jPlayer("play");
        });
    },
    swfPath: "/ui/core/js/jPlayer/",
    supplied: "mp3",
    wmode: "window"
});

});

here is the html:

<table>
    <tr>
      <td>
        <a href="music.mp3" class="voice">Listen</a>
      </td>
    </tr>
</table>
<div id="jquery_jplayer"></div>

what am i missing?

thanks


Solution

  • there were a couple of things..

    1. this, when used within a click event refers to the clicked element, not your jPlayer element
    2. there is no such jPlayer method as setFile - it's setMedia

    try this:

    $(document).ready(function(){
        $("#jquery_jplayer").jPlayer({
            swfPath: "/ui/core/js/jPlayer/",
            supplied: "mp3",
            wmode: "window"
        });
    
        $('.voice').click(function(e) {
            e.preventDefault();
            $("#jquery_jplayer")
                .jPlayer("setMedia", {mp3: this.href })
                .jPlayer("play");
        });
    });