简体   繁体   中英

Make stop audio button

So I have code that looks like this. I press the button and an mp3 plays. But I can't seem to find a way to make a stop button for this and was hoping y'all could help me.

<a href="#/" id="song1" class="buttons">Name of Song 1</a>
<a href="#/" id="song2" class="buttons">Name of Song 2</a>

var audios = 
Array.prototype.map.call(document.getElementsByClassName("buttons")
, function (el) {
    var audio = new Audio();
    var src = el.id + ".mp3";
    el.onclick = function () {
        audio.src = src;
        audio.play();
    };
    return audio;
});

I tried

function stopmusic() {
    audio.pause();
}
document.getElementById('stopbuttons').addEventListener('click', stopmusic);

and

function stopmusic() {
 audios.forEach(audio=>audio.pause());
 audios.forEach(audio=>audio.currentTime = 0.0);
 audios.forEach(audio=>audio.src = "");
 }
document.getElementById('stopbutton').addEventListener('click', stopmusic);

but couldnt get it to work

use audio.pause(); to stop the audio

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM