10

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?

0

3 Answers 3

31

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);
Sign up to request clarification or add additional context in comments.

Comments

4

Set the loop="loop" attribute in the audio tag.

Comments

2

The audio element has a loop boolean property that you can set to auto-loop its playback. Another option would be to add and event listener which responds to the "ended" event of the audio element. In your handler, set the position of the audio back to 0, and play anew.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.