So this is my first post requesting for some help. I have dove in the deep end while building my first website by adding a music player to it. I've taken code found online and done my best to tie it into my own. So far most of it is working. The play pause works, previous and back also works.
I think my problem lies within songIndex? When I 'next' after track2 it only goes back to track1. When I try and 'previous' the code shows 'src"undefined"'and no song will play. Not sure if they are related.
Here is the code I belive to be relivant. Any and all help is appreciated. I am only starting off learning Javascript.
songIndex = 0;
songs = ['/music/track1.mp3', '/music/track2.mp3', '/music/track3.mp3', '/music/track4.mp3', '/music/track5.mp3', '/music/track6.mp3', '/music/track7.mp3'];
thumbnails = ['/images/J&G Logo.png', '/images/J&G Logo.png', '/images/J&G Logo.png', '/images/J&G Logo.png', '/images/J&G Logo.png', '/images/J&G Logo.png', '/images/J&G Logo.png', ];
songArtists = ['Jelly & The GOAT', 'Jelly & The GOAT', 'Jelly & The GOAT', 'Jelly & The GOAT', 'Jelly & The GOAT', 'Jelly & The GOAT', 'Jelly & The GOAT',];
songTitles = ["Track1", "Track2", "Track3", "Track4", "Track5", "Track6", "Track7"];
I will skip some lines of code as I don't think they are bugged.
song.addEventListener('ended', function(){
nextSong();
});
function nextSong() {
songIndex++;
if (songIndex > 1) {
songIndex = 0;
};
song.src = songs[songIndex];
thumbnail.src = thumbnails[songIndex];
background.src = thumbnails[songIndex];
songArtist.innerHTML = songArtists[songIndex];
songTitle.innerHTML = songTitles[songIndex];
playing = true;
playPause();
}
function previousSong() {
songIndex--;
if (songIndex < 0) {
songIndex = 1;
};
song.src = song[songIndex];
thumbnail.src = thumbnails[songIndex];
background.src = thumbnails[songIndex];
songArtist.innerHTML = songArtists[songIndex];
songTitle.innerHTML = songTitles[songIndex];
playing = true;
playPause();
}
Once again, thanks for any help. One thing I noticed (with the two tracks that are working) if I skip to the next song whilst its playing, the song doesn't auto play and the pause button is still showing (requiring a double click)