Javascript DOM HTML Audio autoplay Property set

Introduction

The autoplay property sets or gets whether the audio should start playing when ready.

This property mirrors the <audio> autoplay attribute.

The <audio> autoplay attribute sets if the audio should automatically play when loaded.

Property Values

Value Description
truethe audio should start playing as soon as it is loaded
false Default. the audio should NOT start playing as soon as it is loaded

Enable autoplay, and reload the video:

var x = document.getElementById("myAudio"); 
x.autoplay = true; 
x.load();

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>

<button onclick="enableAutoplay()" type="button">Enable autoplay</button>
<button onclick="disableAutoplay()" type="button">Disable autoplay</button>
<button onclick="checkAutoplay()" type="button">Check autoplay status</button>
<p id="demo"></p>
<script>
var x = document.getElementById("myAudio");

function enableAutoplay() { /*from   w  ww  .j a  va2 s  .  c o m*/
  x.autoplay = true;
  x.load();
}
 
function disableAutoplay() { 
  x.autoplay = false;
  x.load();
} 
  
function checkAutoplay() { 
  document.getElementById("demo").innerHTML = x.autoplay;
} 
</script> 

</body>
</html>



PreviousNext

Related