Answer the question
In order to leave comments, you need to log in
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;
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question