Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop audio with JavaScript

I have the next code

var audioElement0 = document.createElement('audio');
audioElement0.setAttribute('src', 'notify.wav');
audioElement0.setAttribute('autoplay', 'autoplay');
audioElement0.Play(); 

var audioElement1 = document.createElement('audio');
audioElement1.setAttribute('src', 'notify.wav');
audioElement1.setAttribute('autoplay', 'autoplay');
audioElement1.Play(); 

var audioElement2 = document.createElement('audio');
audioElement2.setAttribute('src', 'notify.wav');
audioElement2.setAttribute('autoplay', 'autoplay');
audioElement2.Play(); 

but it only plays once... How can I fix it?

like image 916
MGE Avatar asked Nov 30 '22 13:11

MGE


1 Answers

You have the loop property:

audioElement.loop=true;

But some browsers do not support well the loop property, you can add an event listener like this:

audioElement.addEventListener('ended', function() {
    this.currentTime = 0;
    this.play();
}, false);
like image 81
xavier.seignard Avatar answered Dec 06 '22 22:12

xavier.seignard