Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Henzly Meghie
Henzly Meghie
4,707 Points

Audio Playlist

I am trying to build a webpage that can play multiple playlist(albums). The problem I have is that when a song ends on the other playlist the next songs continues playing from the playlist(album) before the one that was currently playing.

Here is the JavaScript code I got from YouTube.

function audioPlayer(){ var currentSong = 0; $("#audioPlayer")[0].src = $("#playlist li a")[0]; $("#playlist li a").click(function(e){ e.preventDefault(); $("#audioPlayer")[0].src = this; $('.playp').addClass('hide'); $('.pausep').removeClass('hide'); $("#audioPlayer")[0].play(); $("#playlist li").removeClass("current-song"); currentSong = $(this).parent().index(); $(this).parent().addClass("current-song"); });

$("#audioPlayer")[0].addEventListener("ended", function(){
   currentSong++;

    $("#playlist li").removeClass("current-song");
    $("#playlist li:eq("+currentSong+")").addClass("current-song");
    $("#audioPlayer")[0].src = $("#playlist li a")[currentSong].href;
    $("#audioPlayer")[0].play();
});

}

1 Answer

Steven Parker
Steven Parker
229,732 Points

Without knowing what the rest of the code is doing, this is a bit of a guess.

The variable currentSong is reset when the audioPlayer function runs.

So when does it run?