M
M
Megas2013-06-04 17:21:12
JavaScript
Megas, 2013-06-04 17:21:12

JavaScript audio looping?

There is a task to play some short sound (sample) in an endless loop.
For this, the simplest code is written:

var audio = new Audio();
audio.src = 'sound.ogg';
audio.loop = true;
audio.autoplay = true;

However, a problem arises when, after the Audio element plays the sound, it takes some time to start all over again. The result is a "hiccup" sound.
Here is an example of what happens
Checked in chrome and firefox, everywhere the same problem.
Does anyone know how this can be resolved?
PS There are no problems with the sample, when playing, for example, in Winamp, everything turns out fine.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Mithgol, 2013-06-04
@Megas

Try howler.js .

M
Max, 2013-06-04
@AloneCoder

Watch this

A
Alyosha, 2020-04-12
@peredozo

A crutch through requestAnimationFrame - it works out much more often than ontimeupdate, therefore it gives a more acceptable result. Well, and accordingly, it devours a little more percent.

var a = new Audio('pdz.me.ogg');
a.loop = true;
function loopFix() {
  if(a.currentTime >= a.duration - 0.05) {
    a.currentTime = 0;
    a.play();
  }
  requestAnimationFrame(loopFix);
}
loopFix();
// ---
a.play();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question