I need to write code where, when you click on an image, an audio file starts playing. If it is clicked again the music stops.
I tried to do it with this code:
<audio id="audioFile" src="audio/sunflower.mp3"> </audio>
<button id="player">
<img src="images/cassetta.jpg">
</button>
document.addEventListener('DOMContentLoaded', function() {
let playing = false;
document.getElementById('player').addEventListener('click', function() {
let audio = document.getElementById('audioFile');
if (playing == false) {
s.play();
playing = true;
} else {
s.pause();
playing = false;
}
});
});
New contributor
pietro is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1