What's the JavaScript API for checking if an HTML5 audio element is currently playing?
16 Answers
function isPlaying(audioEl) {
return !audioEl.paused;
}
The Audio tag has a paused property. If it is not paused, then it's playing.
8 Comments
paused attribute represents whether the media element is paused or not. The attribute must initially be true". Maybe @Harry and @Tomas found this not to be the case in 2012 because the specs were not clear at that point. BUT ... I couldn't find anything in the specs (not to say it's not there!) that says that the 'paused` attribute must be true when playback ends. Any views?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.
}
4 Comments
!myAudio.paused||!myAudio.currentTime would do a better job.!myAudio.paused || myAudio.currentTime ?!myAudio.paused || myAudio.currentTime. Never replied back it seems...duration can be NaN, so some boolean checks on duration may behave erraticallyWhile 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.
1 Comment
document.getElementsByTagName('audio').addEventListener('playing',function() { myfunction(); },false);
Should do the trick.
1 Comment
getElementsByTagName('audio')[0] because it returns collection, not one elementI wondered if this code would work, and it amazingly did:
if (a.paused == false) {
/*do something*/
}
or you could write it as:
if (!a.paused == true) {
/*do something*/
}
if you want your IDE annoying you in JSfiddle.
2 Comments
you can to use the onplay event.
var audio = document.querySelector('audio');
audio.onplay = function() { /* do something */};
or
var audio = document.querySelector('audio');
audio.addEventListener('play', function() { /* do something */ };
1 Comment
Try Out This Code:
var myAudio = document.getElementById("audioFile");
function playAudio() {
//audio.play();
//console.log(audio.play())
if (myAudio.paused && myAudio.currentTime >= 0 && !myAudio.started) {
myAudio.play();
console.log("started");
} else {
myAudio.pause();
}
}
1 Comment
I do this way
<button id="play">Play Sound</button>
<script>
var sound = new Audio("path_to_sound.mp3")
sound.loop = false;
var play = document.getElementById("play")
play.addEventListener("click", () => {
if (!isPlaying()) {
sound.play()
} else {
//sound.pause()
}
})
function isPlaying() {
var infoPlaying = false
var currentTime = sound.currentTime == 0 ? true : false
var paused = sound.paused ? true : false
var ended = !sound.ended ? true : false
var readyState = sound.readyState == 0 ? true : false
if (currentTime && paused && ended && readyState) {
infoPlaying = true
} else if (!currentTime && !paused && ended && !readyState) {
infoPlaying = true
}
return infoPlaying
}
</script>
Comments
There is an answer by @AllanRibas, in which the function should rather look like this in my opinion:
let plr = document.querySelector("#player-audio-element");
if(isPlaying(plr) === true) {
// do stuff ...
}
export function isPlaying(plr) {
let atStart = plr.currentTime == 0 ? true : false
if ((atStart && plr.paused) || (plr.ended && plr.readyState == 0))
return false
return true
}
Comments
I was able to solve the problem using Singletone
let instance: HTMLAudioElement;
export class SingletoneAudio {
public audioPath: string;
constructor(audioPath: string) {
this.audioPath = audioPath;
if (!instance) {
instance = new Audio(audioPath);
}
}
getInstance() {
return instance;
}
}
const audioInstance = new SingletoneAudio('your-path');
const audio = audioInstance.getInstance();
audio.loop = true;
Comments
I don't know why this could be wrong:
var isPlaying = false;
audio = document.getElementById('audioID');
audio.addEventListener('playing',function() { isPlaying = true; },false);
audio.addEventListener('pause', function() { isPlaying = false; },false);
audio.addEventListener('ended', function() { isPlaying = false; },false);
Comments
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
});
Comments
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.