Loop Through a Playlist
The first thing you’ll need for your music app is two variables.
- The
currentTrackNumber
variable keeps track of which song is playing right now - The
listOfTracks
is your playlist where you’ll store your songs
The next thing you need is to make the Play button load up the first track
To help you imagine what’s going on here, you can think of it being like a juke box selecting a record. You’re queuing up something to play, but it’s not ready to play…yet!
This is the block that allows us to make the playlist app function.
Once the audio is ready to be played the on source loaded
event fires.
The first thing we want to do is Play the track that we’ve selected:
These final blocks set up the looping playlist for us.
- When the first song finishes playing we increase the
currentTrackNumber
by 1 - We check if there are any tracks left on the playlist, and if not we loop back to the beginning
- Finally a new source track is loaded, which in turn will trigger a new
on source loaded
event and the cycle will continue