0

An application is playing an audio clip with the HTMLAudioElement nested in some JavaScript like below,

var s = new Audio('/sounds/s.wav');
s.play();

HTMLAudioElement docs: https://developer.mozilla.org/en-US/docs/Web/API/HTMLAudioElement

The problem is that the clip needs to play every time a user takes an action. But if the previous audio clip is still playing then the next clip will not play (be delayed or skip completely).

So is there a way to cutoff an audio clip, mid way while it's playing?

Something like s.stop()?


UPDATE

Maybe this is still a duplicate question...

There are multiple audio clips for different actions so if implemented sound.pause(); and sound.currentTime = 0; from referenced duplicate question, it might look something like this, which could be run before each clip.

function stopSounds() {
    sound1.pause();
    sound1.currentTime = 0;

    sound2.pause();
    sound2.currentTime = 0;

    sound3.pause();
    sound3.currentTime = 0;

    sound4.pause();
    sound4.currentTime = 0;

    sound5.pause();
    sound5.currentTime = 0;

    sound6.pause();
    sound6.currentTime = 0;

    sound7.pause();
    sound7.currentTime = 0;
}
tim_xyz
  • 11,573
  • 17
  • 52
  • 97

2 Answers2

0

The function you're searching for is s.pause(); If you want to go to the beginning of the file if it's started again, then you have to set s.currentTime = 0; (as I know it starts playing immediately then, at least in Chrome).

RafaelKr
  • 915
  • 8
  • 18
0

Just Call this Function:

  var s = new Audio('/sounds/s.wav');
  s.play(); 
  s.pause();
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Shantanu Madane
  • 617
  • 5
  • 14