What I want is to play and pause any track selected
But what I am getting
- Plays when you click on play but when you click on the same button again it stops working and no track can be selected to play again.
// Grabbed 3 play buttons from Dom
const pausePlayBtn = document.querySelectorAll('.play img')
const songs = ["Song1.mp3", "Song2.mp3", "Song3.mp3"]; //The songs
const song = new Audio();
pausePlayBtn.forEach((btn, i) => {
btn.addEventListener('click', function () {
song.src = songs[i];
togglePlay(song)
})
})
let isPlaying = false;
function togglePlay(s) {
if (isPlaying) {
s.pause()
} else {
s.play();
}
};
song.onplaying = function () {
isPlaying = true;
};
song.onpause = function () {
isPlaying = false;
};
Just incase you get confused this the full code on github.
I really need this piece of code to work for my on going project. Thanks