Check if audio is playing without HTML5 tag

html, javascript

Solution

This should work:

var sound = new Audio('https://mfaucet.com/images/notification2.mp3');

function play(sound) {
  if(!sound.paused) sound.pause();
  sound.currentTime = 0;
  sound.play();
}
<button onclick="play(sound)">Play</button>Press two times

Explanation:

- When the page loads, the Audio variable is created, and the page can load the mp3 beforehand.

- When the user clicks a button the play-function will:

- Check if the sound is playing, and pause it if it is.

- Set the time to 0.

- Start playing

Problem

I want to detect if an audio is playing, in case it is, don't allow to play it again until it finish. Play the same audio twice causes an audio bug. HTML ``` <button onclick="play()">Play</button> Press two times ``` Javascript: ``` function play() { var sound = new Audio('https://mfaucet.com/images/notification2.mp3'); console.log('Paused: ' + sound.paused); console.log('Ended: ' + sound.ended); console.log('Current time: ' + sound.currentTime); sound.play(); console.log('-----------------'); console.log('Paused: ' + sound.paused); console.log('Ended: ' + sound.ended); console.log('Current time: ' + sound.currentTime); console.log('-----------------------------'); } ``` Online: http://jsbin.com/hageko/1/ Thanks.

Original source