👀 Live Preview
Press play — watch play vs playing fire in order:
Ready — press play

The onplaying attribute is an inline event handler for the playing event on audio or video elements. It runs when media is actively outputting sound or frames — after buffering finishes or after resuming from pause. This is different from onplay, which fires when playback is requested. Use onplaying to hide loading spinners, start synced animations, or confirm real playback. Pair with onwaiting for buffering UX. Prefer addEventListener("playing", …) in production.
Inline JS.
Active output.
Media only.
Later event.
Buffer pair.
Preferred way.
onplaying AttributeThe primary purpose of onplaying is to react when media is truly playing — not just when the user clicked play. After a slow network, the browser may fire play first, then waiting while buffering, then playing when data is ready. Your handler on playing is the right moment to hide spinners and start time-synced effects.
The playing event also fires when playback resumes after being paused and data is available again. It does not fire when media reaches the end — use onended for that.
onplay = requested. onwaiting = buffering. onplaying = actually outputting. On fast connections, play and playing may fire almost together.
Set onplaying on audio or video, or assign media.onplaying in JavaScript:
<audio controls onplaying="handlePlaying()" onwaiting="showSpinner()">
<source src="/audio/count.mp3" type="audio/mpeg">
</audio>
<script>
media.onplaying = handlePlaying;
</script>playing event fires.<audio> and <video> elements.element.onplaying in script.onwaiting to show/hide buffering UI.onplay — fires later when output starts.media.addEventListener("playing", handler).The onplaying attribute accepts a string of JavaScript code:
onplaying="handlePlaying()" — Call a named function.onplaying="spinner.hidden = true" — Hide loader inline.video.onplaying = () => { … } — property assignment.function handlePlaying() {
spinner.hidden = true;
status.textContent = "Now playing";
}
function showSpinner() {
spinner.hidden = false;
status.textContent = "Buffering…";
}
media.addEventListener("playing", handlePlaying);
media.addEventListener("waiting", showSpinner);| Property / event | When it fires | Notes |
|---|---|---|
playing | Media actively outputting | onplaying |
play | Playback requested | onplay (earlier) |
waiting | Buffering, stalled | onwaiting |
pause | Playback paused | onpause |
ended | Media finished | onended |
| Element | Supported? | Notes |
|---|---|---|
<audio> | Yes | Primary use case |
<video> | Yes | Same playing event |
<input>, <div> | No | Not media elements |
<iframe> (embed) | No | Cross-origin limits |
| Custom player UI | Related | Listen on underlying media |
onplaying vs onplay vs onwaiting| Attribute | Event | Typical use |
|---|---|---|
onplay | play | User pressed play |
onwaiting | waiting | Show buffering spinner |
onplaying | playing | Hide spinner, sync animation |
onpause | pause | Stop synced effects |
Inline audio handler, dynamic assignment, and play vs playing status comparison.
Press play — watch play vs playing fire in order:
Ready — press play
Show feedback when media is actively playing:
<audio controls onplaying="handlePlayingEvent()">
<source src="/audio/count.mp3" type="audio/mpeg">
</audio>
<p id="status">Ready</p>
<script>
function handlePlayingEvent() {
document.getElementById("status").textContent =
"Now playing";
}
</script>The browser fires playing when media actually outputs. The inline attribute runs handlePlayingEvent() at that moment.
Assign element.onplaying after the page loads:
<audio controls id="myAudio">
<source src="/audio/count.mp3" type="audio/mpeg">
</audio>
<script>
document.getElementById("myAudio").onplaying = function () {
document.getElementById("log").textContent =
"Media is now playing!";
};
</script>Property assignment on the audio element is equivalent to an inline onplaying attribute.
Pair onwaiting and onplaying for buffering UX:
media.addEventListener("waiting", function () {
spinner.hidden = false;
status.textContent = "Buffering…";
});
media.addEventListener("playing", function () {
spinner.hidden = true;
status.textContent = "Playing";
});onplaying confirms data is flowing — safe to hide the loader and start playback-synced UI.
aria-live="polite" when status changes between waiting and playing.playing fires if needed.play fires.
waiting maybe.
onplaying runs.
Hide loader.
The playing event and onplaying handler are supported wherever HTML5 audio and video are supported — all modern browsers.
onplaying supportAll major browsers fire playing on audio and video elements.
Bottom line: Reliable for buffering and playback-sync UX in all modern browsers.
onplaying with onwaitingonplaying to hide loading spinnersaddEventListener("playing", …) in productionplayingonplayplaying with playonplaying for “user clicked play” onlyonplaying on non-media elementsplaying means media endedThe onplaying attribute runs JavaScript when media is actively outputting — after buffering or resume — essential for hiding loaders and syncing playback UI.
Pair it with onwaiting, distinguish it from onplay, and prefer addEventListener in production code.
onplayingBookmark these before handling media playback events.
Not just play.
EventLater than onplay.
OrderBuffer pair.
PatternClassic UX.
UXMid-stream.
Gotchaplaying event fires — when media is actively outputting after buffering or resume.onplay fires when playback is requested. onplaying fires when media is actually playing — often slightly later.<audio> and <video> — HTML5 media elements only.media.addEventListener("playing", handler) is preferred for production code.Practice the onplaying attribute with audio and buffering examples in the Try It editor.
5 people found this page helpful