What's the javascript api for checking if an html5 audio element is currently playing?
138
votes
11 Answers
181
votes
function isPlaying(audelem) { return !audelem.paused; }
The Audio tag has a paused
property. If it is not paused, then it's playing.
51
votes
18
votes
While I am really late to this thread, I use this implementation to figure out if the sound is playing:
service.currentAudio = new Audio();
var isPlaying = function () {
return service.currentAudio
&& service.currentAudio.currentTime > 0
&& !service.currentAudio.paused
&& !service.currentAudio.ended
&& service.currentAudio.readyState > 2;
}
I think most of the flags on the audio element are obvious apart from the ready state which you can read about here: MDN HTMLMediaElement.readyState.
12
votes
5
votes
2
votes
1
votes
1
votes
1
votes
0
votes
am using this jquery code withe tow button play and stop, play button is a play and pouse button
const help_p = new Audio("audio/help.mp3");//Set Help Audio Name
$('#help_play').click(function() {//pause-Play
if (help_p.paused == false) {
help_p.pause();//pause if playing
} else {
help_p.play();//Play If Pausing
}
});
$('#help_stop').click(function() {//Stop Button
help_p.pause();//pause
help_p.currentTime = 0; //Set Time 0
});
-2
votes
While there is no method called isPlaying or something similar, there are a few ways to accomplish this.
This method gets the % of progress as audio is playing:
function getPercentProg() {
var myVideo = document.getElementById('myVideo');
var endBuf = myVideo.buffered.end(0);
var soFar = parseInt((endBuf / myVideo.duration) * 100);
document.getElementById('loadStatus').innerHTML = soFar + '%';
}
If percent is greater than 0 and less than 100, it is playing, else it is stopped.