Search code examples
javascriptjqueryjplayer

How to get the total number of tracks on jPlayer


I'm currently adding and deleting tracks from my playlist dynamically with playlist.add() and playlist.remove(). Once there are no more tracks left in the playlist, I want to hide the player div container, effectively hiding the player itself. So, everytime I remove a track, I want to check for the total number of tracks currently in the playlist. Once it reaches 0, I should be able to hide the container.

Some codes (though I feel it is not necessary):

Setting the playlist:

    var maPlaylist = new jPlayerPlaylist({
                jPlayer: "#jquery_jplayer_1",
                cssSelectorAncestor: "#jp_container_1"
            }, [], {
                swfPath: "js",
                supplied: "oga, mp3",
                wmode: "window",
                smoothPlayBar: true,
                keyEnabled: true
            });     

Adding to playlist:

maPlaylist.add({
title: trackTitle,
mp3: "path/" + track.uid + "/" + track.filename,
    oga: "path/" + track.uid + "/" + track.filename
}); 

Removing from playlist:

if( maPlaylist.remove(arrayPos) )
    alert("Track removed successfully!");
else
    alert("Failed to remove track from the playlist!"); 

Solution

  • The answer was actually stupidly simple, staring me in the face all along.

    The playlist is an array (of course! durr!). So, it was just a matter of getting the length of the array. Like so:

        console.log(maPlaylist.playlist.length);
    

    I hope this helps someone in the future.