HTML5 check if audio is playing?
You can check the duration. It is playing if the duration is more than 0 seconds and it is not paused.
var myAudio = document.getElementById('myAudioID');
if (myAudio.duration > 0 && !myAudio.paused) {
//Its playing...do your job
} else {
//Not playing...maybe paused, stopped or never played.
}
document.getElementsByTagName('audio').addEventListener('playing',function() { myfunction(); },false);
Should do the trick.
function isPlaying(audelem) { return !audelem.paused; }
The Audio tag has a paused
property. If it is not paused, then it's playing.
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.