Javascript DOM HTML Audio Object get

Introduction

The Audio object represents an HTML <audio> element.

We can access an <audio> element by using getElementById():

var x = document.getElementById("myAudio");

Click the button to get the duration of the audio, in seconds.

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>/* w  ww  . j av a 2  s.c  o m*/
<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {
  var x = document.getElementById("myAudio").duration;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>



PreviousNext

Related