4

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

khinester
  • 3,398
  • 9
  • 45
  • 88

1 Answers1

5

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");
    });
});
Lloyd
  • 8,204
  • 2
  • 38
  • 53