Coder Perfect

Is there a way to tell if audio is playing in HTML5?


If an html5 audio element is currently playing, what should you do?

Asked by Harry

Solution #1

function isPlaying(audelem) { return !audelem.paused; }

The paused feature is available in the Audio tag. It is playing if it is not paused.

Answered by Niet the Dark Absol

Solution #2

You can see how long it takes. If the duration is greater than 0 seconds and it is not paused, it is playing.

var myAudio = document.getElementById('myAudioID');

if (myAudio.duration > 0 && !myAudio.paused) {

    //Its your job

} else {

    //Not playing...maybe paused, stopped or never played.


Answered by Maxali

Solution #3

While I’m a little late to the party, I use the following implementation to determine whether or not 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;

Apart from the ready state, which you can learn about here: MDN HTMLMediaElement.readyState, I believe most of the flags on the audio element are self-evident.

Answered by Hassan Mahmud

Solution #4

document.getElementsByTagName('audio').addEventListener('playing',function() { myfunction(); },false); 

That should suffice.

Answered by projectxmatt

Solution #5

Take a look at this feature! If the location is at the start or finish of the audio, it will not be played.

function togglePause() {
     if (myAudio.paused && myAudio.currentTime > 0 && !myAudio.ended) {;
     } else {

Answered by Nidlol Masyhud

Post is based on