Javascript DOM HTML Audio loop Property

Introduction

Set the audio to loop:

document.getElementById("myAudio").loop = true;

View in separate window

<!DOCTYPE html> 
<html> 
<body> 

<audio id="myAudio" controls>
  <source src="sound.ogg" type="audio/ogg">
  <source src="sound.mp3" type="audio/mpeg">
  Your browser does not support the audio element.
</audio><br>
<p id="demo"></p>
<button onclick="enableLoop()" type="button">Enable loop</button>
<button onclick="disableLoop()" type="button">Disable loop</button>
<button onclick="checkLoop()" type="button">Check loop status</button>

<script>
var x = document.getElementById("myAudio");

function enableLoop() { /*w  w w. j a v  a 2s . c  o  m*/
  x.loop = true;
  x.load();
} 

function disableLoop() { 
  x.loop = false;
  x.load();
} 

function checkLoop() { 
  document.getElementById("demo").innerHTML = x.loop;
} 
</script> 

</body> 
</html>

The loop property sets or gets if an audio will play again when finished.

This property mirrors the <audio> loop attribute.

When present, the audio will play again when it is finished.

Property Values

Value Description
true the audio should start playing again when it is finished
false Default. the audio should NOT start playing again when it is finished



PreviousNext

Related