I’m trying to build a play/pause all-in-one button in javascript. Determined by the condition of aud_play_pause rather than the click event. Ideally, it can switch to the correct class/text (audio.play -> icon “pause”-> text “Pause”/ audio.paused ->icon “play”->text “Play” audio.buffering -> text "Loading") by itself without affection from auto-play function (just in case). Is that possible for the text displays "loading" during the buffering process by javascript?
Please excuse my poor coding skill and find the complete code from here jsfinddle link
var audio = new Audio(),
u = 0;
var playlist = new Array('http://www.w3schools.com/htmL/horse.mp3', 'http://sifidesign.com/audio/explosion.mp3');
audio.addEventListener('ended', function () {
u = ++u < playlist.length ? u : 0;
console.log(u)
audio.src = playlist[u];
audio.play();
}, true);
function aud_play_pause() {
if (audio.paused) {
audio.play();
$('#play span').text('Pause');
$('#play').removeClass('play').addClass('pause');
}
else {
audio.pause();
$('#play span').text('Play');
$('#play').removeClass('pause').addClass('play');
}
}
document.querySelector('#play').addEventListener('click', aud_play_pause)
audio.volume = 0.5;
audio.loop = false;
audio.src = playlist[0];