To check if audio is playing with JavaScript, we can use the paused property of the audio element. const isPlaying = (audElem) => { return !
There are three supported audio formats in HTML: MP3, WAV, and OGG.
The HTML <audio> element is used to play an audio file on a web page.
function isPlaying(audelem) { return !audelem.paused; }
The Audio tag has a paused
property. If it is not paused, then it's 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.
}
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.
document.getElementsByTagName('audio').addEventListener('playing',function() { myfunction(); },false);
Should do the trick.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With