Merge pull request #92 from GabrielPlassard/fixIndexOutOfRange

Fix possible index out of range when auto shuffle is on
This commit is contained in:
Matthieu Grieger 2015-10-14 18:26:05 -07:00
commit 5fcf31f094

View file

@ -124,10 +124,12 @@ func (q *SongQueue) ShuffleSongs() {
// Sets a random song as next song to be played // Sets a random song as next song to be played
// queueWasEmpty wether the queue was empty before adding the last song // queueWasEmpty wether the queue was empty before adding the last song
func (q *SongQueue) RandomNextSong(queueWasEmpty bool){ func (q *SongQueue) RandomNextSong(queueWasEmpty bool){
if (q.Len() > 1){
nextSongIndex := 1 nextSongIndex := 1
if queueWasEmpty{ if queueWasEmpty{
nextSongIndex = 0 nextSongIndex = 0
} }
swapIndex := nextSongIndex + rand.Intn(q.Len()) swapIndex := nextSongIndex + rand.Intn(q.Len() - 1)
q.queue[nextSongIndex], q.queue[swapIndex] = q.queue[swapIndex], q.queue[nextSongIndex] q.queue[nextSongIndex], q.queue[swapIndex] = q.queue[swapIndex], q.queue[nextSongIndex]
}
} }