Skip to content

Instantly share code, notes, and snippets.

@MeoMix
Created November 12, 2012 00:02
Show Gist options
  • Save MeoMix/4056815 to your computer and use it in GitHub Desktop.
Save MeoMix/4056815 to your computer and use it in GitHub Desktop.
//Takes a song's UID and returns the index of that song in the playlist if found.
var getSongIndexById = function(songs, id) {
var songIndex = -1;
for (var i = 0; i < songs.length; i++) {
if (songs[i] && songs[i].id === id) {
songIndex = i;
break;
}
}
return songIndex;
};
//Takes a song and returns the next song object by index.
getNextSong: function() {
var nextSong = null;
var isShuffleEnabled = JSON.parse(localStorage.getItem('isShuffleEnabled')) || false;
if (isShuffleEnabled === true) {
nextSong = playlist.shuffledSongs[0];
} else {
var currentSong = playlist.songHistory[0];
if (currentSong) {
var currentSongIndex = getSongIndexById(playlist.songs, currentSong.id);
var nextSongIndex = currentSongIndex + 1;
//Loop back to the front if at end. Should make this togglable in the future.
if (playlist.songs.length <= nextSongIndex) {
nextSongIndex = 0;
nextSong = playlist.songs[nextSongIndex];
} else {
nextSong = playlist.songs[nextSongIndex];
}
}
}
return nextSong;
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment