Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 check if audio is playing?

People also ask

How do I know if audio element is playing?

To check if audio is playing with JavaScript, we can use the paused property of the audio element. const isPlaying = (audElem) => { return !

Can I play audio in HTML Yes or no?

There are three supported audio formats in HTML: MP3, WAV, and OGG.

What is the correct HTML 5 element for playing audio files?

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.