Search code examples
javascriptjqueryhtmlaudiojplayer

How to play individual track number from playlist at start?


I am using jPlayer plugin. Here is an example link [ jsfiddle ]. I just want to start playlist from an individual track number, i mean if i have 5/10 tracks in a playlist then i want to start playlist from 2/5 track number at start.


Solution

  • You will need to modify a bit the play() method in order to randomly selects the song to play.

    Find below the code and this is an update to your fiddlejs

    var myPlayer = new jPlayerPlaylist( /* give your params here as you were doing */);
    
    // Clonning current play() method behavior
    myPlayer._play = myPlayer.play;
    
    // Modifying default behavior of current play() method which generates
    // a random song index if not provided
    myPlayer.play = function(songIdx){
    
        if (!songIdx)
            songIdx = Math.floor(Math.random() * this.playlist.length);
    
        this._play(songIdx);
    
        console.log('Playing song #', songIdx);
    }