Search code examples
jqueryjplayer

How to get the duration of the song in JPlayer


function intilizePlayer(){ 
    $("#jquery_jplayer_1").jPlayer({
        ready: function (event) {
            $(this).jPlayer("setMedia", {
                oga:song
            });
            songDuration = $(this).jPlayer.status.duration;
        },
        ended: function (event) {
                $(this).jPlayer("play");
        },
        swfPath: "swf",
        supplied: "oga"
    }).bind($.jPlayer.event.play, function() {
        $(this).jPlayer("pauseOthers");
    });
}

Here i try to get the duration of the song. But it says "undefined". Other than this i tried to use the following, after calling the above function.

var duration = $("#jquery_jplayer_1").data("jPlayer").status.duration;

Then the duration became 0. How to get the real duration?


Solution

  • function intilizePlayer(){ 
        $("#jquery_jplayer_1").jPlayer({
            ready: function (event) {
                $(this).jPlayer("setMedia", {
                    oga:song
                });
            },
            ended: function (event) {
                    $(this).jPlayer("play");
            },
            loadeddata: function(event){ // calls after setting the song duration
                songDuration = event.jPlayer.status.duration;
            },
            swfPath: "swf",
            supplied: "oga"
        }).bind($.jPlayer.event.play, function() {
            $(this).jPlayer("pauseOthers");
        });
    }