I have a couple tracks that is programmed by JavaScript. Just One problem that I need to fix and this is when I play one track is working fine and then when I want to play the other track. I want to stoped the first one and then play the other one.
Since there are several things you handle when you click “play” on an audio element, it would be better idea to create a function to handle that. In that function you can do stuff like add the “playing” class, as i see is part of your app design, ofc make the element play and to follow your desire, to make sure no other element is currently playing(stop it, if there is such). Its might be easy to identify such element by targeting the “playing” class with query selector and pause/stop the audio of that element, as well as remove the class off it(assuming only one element is to be playing at any time). You should also be careful of the order in which you run the different actions. Have it clearly thought in your head and even write down on a note, whats to be done after what. For example- look if there is element “playing”, stop/pause that element and remove the class, add the class to the current element that has been clicked, make the element play.
let currentlyPlaying=document.querySelector(".playing")
if (currentlyPlaying) {
currentlyPlaying.pause()
currentlyPlaying.classList.remove("playing")
}
Assuming your code makes sure if there is playing audio, it has the “playing” class