Javascript examples for DOM HTML Element:Audio
The autoplay property sets or gets whether the audio has set autoplay flag by using <audio> autoplay attribute.
Set the autoplay property with the following Values
Value | Description |
---|---|
true|false | Sets whether an audio should automatically start playing as soon as it is loaded |
A Boolean, returns true if the audio automatically starts playing, otherwise it returns false
The following code shows how to Find out if the audio started to play as soon as it was ready:
<!DOCTYPE html> <html> <body> <button onclick="myFunction(true)">Audio with autoplay</button> <button onclick="myFunction(false)">Audio without autoplay</button> <br> <script> function myFunction(p) { var x = document.createElement("AUDIO"); x.setAttribute("id", "myVideo"); x.setAttribute("controls", "controls"); var y = document.createElement("SOURCE"); y.setAttribute("src", "your.ogg"); y.setAttribute("type", "audio/ogg"); x.appendChild(y);// ww w. j a va2s . c om var z = document.createElement("SOURCE"); z.setAttribute("src", "your.mp3"); z.setAttribute("type", "audio/mpeg"); x.appendChild(z); // Set the autoplay property: x.autoplay = p; document.body.appendChild(x); } </script> </body> </html>