I am making an audio player in reactjs that looks like this , song box is rendered in map function and on click on any song box it start playing, but the problem is when click on next song to change the track, the audio src value gets changed but previous song did not stop playing,
any Help?
function playMusic(audio: HTMLAudioElement) {}
function handleMusicPlayer(index: number) {
let audio = new Audio(props.musicData[index].track.preview_url);
audio.play();
playMusic(audio);
if (musicMinimize) {
props.onPlay(false);
setmusicMinimize(false);
setTimeout(() => {
setmusicMinimize(true);
props.onPlay(true);
}, 1000);
} else {
props.onPlay(false);
setmusicMinimize(true);
}
props.onCurrent({
image: props.musicData[index].track.album.images[0].url,
name: props.musicData[index].track.name,
track: props.musicData[index].track.preview_url
});
}
function handleMusicPlayer get run when song box clicked, and function playMusic should control the playing of current and next song