Skip to content Skip to sidebar Skip to footer

Creating Play/pause Functions With Current Audio Javascript

I have an extension for chrome I am working on, I have an HTML popup that has buttons that play audio. I don't think my approach is the most elegant and I am having trouble explori

Solution 1:

var audios= Array.prototype.map.call(document.getElementsByClassName("myButton"),function(el){
    var audio=newAudio();
    audio.preload="none";//=> not preloaded
    audio.src="mp3/"+el.id+".mp3";
    el.onclick=audio.play.bind(audio);
    return audio;
});

Simply iterate over your buttons, create an audio element with the right src for it, and bind the onclick listener to its play function. Then map the audio elements.

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

To stop, simply iterate over the audio elements and call the stop function on each...

Post a Comment for "Creating Play/pause Functions With Current Audio Javascript"