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 |
---|---|
true | the audio should start playing as soon as it is loaded |
false | Default. the audio should NOT start playing as soon as it is loaded |
Get whether the audio started to play as soon as it was ready:
var x = document.getElementById("myAudio").autoplay;
Click the button to get if the audio automatically started to play as soon as it was loaded.
<!DOCTYPE html> <html> <body> <audio id="myAudio" controls autoplay> <source src="sound.ogg" type="audio/ogg"> <source src="sound.mp3" type="audio/mpeg"> Your browser does not support the audio element. </audio>/*from w w w. j a v a 2 s . c o m*/ <button onclick="myFunction()">Test</button> <p id="demo"></p> <script> function myFunction() { var x = document.getElementById("myAudio").autoplay; document.getElementById("demo").innerHTML = x; } </script> </body> </html>