In my audio player, i load a song by default and then hit play button to play the audio. Now when i hit the next song button, it loads the next song but does not play it. I Have to again hit pause/play button to play it. Same thing with playing next song when previous song has ended. So, the player requires interaction to play the song after its loaded. How can i autoplay next (or previous) songs without having to interact with the player each time?
exports.songsdata = [
{
"title": "song1",
"url": "https://song1.mp3"
},
{
"title": "song2",
"url": "https://song2.mp3"
},
{
"title": "song3",
"url": "https://song3.mp3"
}
]
const [currentSong, setCurrentSong] = useState(songsdata[0]);
const playNext = () => {
const index = songs.findIndex(x=>x.title == currentSong.title);
if (index == songs.length-1)
{
setCurrentSong(songs[0]);
}
else
{
setCurrentSong(songs[index + 1]);
}
}
<div className="AudioPlayer">
<audio src={currentSong.url} />
</div>
<div className="controls">
<Button className='btn_action' onClick={playNext} />
</div>