Audio readyState Property - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Audio

Description

The readyState property returns the current ready state of the audio, which tells if the audio is ready to play or not.

This property is read-only.

Return Value

Type Description
Number Represents the ready state of the audio element:

Which could have the following value:

  • 0 = HAVE_NOTHING - no information
  • 1 = HAVE_METADATA - metadata is ready
  • 2 = HAVE_CURRENT_DATA - data for the current position is available, but not enough data to play next frame
  • 3 = HAVE_FUTURE_DATA - data for the current and at least the next frame is available
  • 4 = HAVE_ENOUGH_DATA - enough data available to start playing

The following code shows how to get the current ready state of the 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>/*from  www.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").readyState;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

Related Tutorials