files/en-us/web/api/htmlmediaelement/readystate/index.md
{{APIRef("HTML DOM")}}
The HTMLMediaElement.readyState property indicates the
readiness state of the media.
A number which is one of the five possible state constants defined on the {{domxref("HTMLMediaElement")}} interface:
HTMLMediaElement.HAVE_NOTHING (0)
HTMLMediaElement.HAVE_METADATA (1)
HTMLMediaElement.HAVE_CURRENT_DATA (2)
HTMLMediaElement.HAVE_FUTURE_DATA (3)
HTMLMediaElement.HAVE_ENOUGH_DATA (4)
This example will listen for audio data to be loaded for the element example. It will
then check if at least the current playback position has been loaded. If it has, the
audio will play.
<audio id="example" preload="auto">
<source src="sound.ogg" type="audio/ogg" />
</audio>
const obj = document.getElementById("example");
obj.addEventListener("loadeddata", () => {
if (obj.readyState >= HTMLMediaElement.HAVE_CURRENT_DATA) {
obj.play();
}
});
{{Specifications}}
{{Compat}}
HTMLMediaElement.readyState property