Audio autoplay Property - create a <audio> element and setting the autoplay property: - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Audio

Description

Audio autoplay Property - create a <audio> element and setting the autoplay property:

Demo Code

ResultView the demo in separate window

<!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);//  w w  w  .ja  va2 s.  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>

Related Tutorials