Audio duration Property - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Audio

Description

The duration property returns the length of an audio, in seconds.

This property is read-only.

Return Value

A Number, representing the length of the audio, in seconds.

If no audio is set, "NaN" (Not-a-Number) is returned.

If the audio is streamed and has no predefined length, "Inf" (Infinity) is returned.

The following code shows how to Get the length of an audio:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<audio id="myAudio" controls>
  <source src="your.ogg" type="audio/ogg">
  <source src="your.mp3" type="audio/mpeg">
  Your browser does not support the audio element.
</audio>/*  w  ww. j a  v  a  2 s.c o m*/

<button onclick="myFunction()">get the exact length (duration) of the audio, in seconds</button>

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

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

</body>
</html>

Related Tutorials