HTML onplaying Attribute

Beginner
⏱️ 6 min read
📚 Updated: Jun 2026
🎯 3 Examples
Events & Handlers

Introduction

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.

What You’ll Learn

01

Event handler

Inline JS.

02

playing

Active output.

03

audio / video

Media only.

04

vs onplay

Later event.

05

+ onwaiting

Buffer pair.

06

addEventListener

Preferred way.

Purpose of onplaying Attribute

The 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.

💡
Event order: play → waiting? → playing

onplay = requested. onwaiting = buffering. onplaying = actually outputting. On fast connections, play and playing may fire almost together.

📝 Syntax

Set onplaying on audio or video, or assign media.onplaying in JavaScript:

onplaying.html
<audio controls onplaying="handlePlaying()" onwaiting="showSpinner()">
  <source src="/audio/count.mp3" type="audio/mpeg">
</audio>

<script>
  media.onplaying = handlePlaying;
</script>

Syntax Rules

  • Value is JavaScript executed when the playing event fires.
  • Valid on <audio> and <video> elements.
  • Also assignable as element.onplaying in script.
  • Pair with onwaiting to show/hide buffering UI.
  • Not the same as onplay — fires later when output starts.
  • Modern alternative: media.addEventListener("playing", handler).
  • Not for non-media elements.

💎 Values

The onplaying attribute accepts a string of JavaScript code:

  • onplaying="handlePlaying()" — Call a named function.
  • onplaying="spinner.hidden = true" — Hide loader inline.
  • JavaScript: video.onplaying = () => { … } — property assignment.
onplaying-handler.html
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);

⚡ Quick Reference

Property / eventWhen it firesNotes
playingMedia actively outputtingonplaying
playPlayback requestedonplay (earlier)
waitingBuffering, stalledonwaiting
pausePlayback pausedonpause
endedMedia finishedonended

Applicable Elements

ElementSupported?Notes
<audio>YesPrimary use case
<video>YesSame playing event
<input>, <div>NoNot media elements
<iframe> (embed)NoCross-origin limits
Custom player UIRelatedListen on underlying media

onplaying vs onplay vs onwaiting

AttributeEventTypical use
onplayplayUser pressed play
onwaitingwaitingShow buffering spinner
onplayingplayingHide spinner, sync animation
onpausepauseStop synced effects

Examples Gallery

Inline audio handler, dynamic assignment, and play vs playing status comparison.

👀 Live Preview

Press play — watch play vs playing fire in order:

Ready — press play

Example — onplaying on audio

Show feedback when media is actively playing:

audio-onplaying.html
<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>
Try It Yourself

How It Works

The browser fires playing when media actually outputs. The inline attribute runs handlePlayingEvent() at that moment.

Dynamic Values with JavaScript

Assign element.onplaying after the page loads:

dynamic-onplaying.html
<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>
Try It Yourself

How It Works

Property assignment on the audio element is equivalent to an inline onplaying attribute.

Example — Hide spinner on playing

Pair onwaiting and onplaying for buffering UX:

onplaying-spinner.html
media.addEventListener("waiting", function () {
  spinner.hidden = false;
  status.textContent = "Buffering…";
});

media.addEventListener("playing", function () {
  spinner.hidden = true;
  status.textContent = "Playing";
});
Try It Yourself

How It Works

onplaying confirms data is flowing — safe to hide the loader and start playback-synced UI.

♿ Accessibility

  • Announce buffering — Use aria-live="polite" when status changes between waiting and playing.
  • Do not rely on spinners alone — Include text like “Loading” or “Playing”.
  • Keep native controls — Or provide accessible custom play/pause buttons.
  • Captions for video — Sync caption tracks when playing fires if needed.
  • Respect autoplay policy — User gesture may be required before play/playing occur.

🧠 How onplaying Works

1

User presses play

play fires.

onplay
2

Buffering?

waiting maybe.

Spinner
3

playing fires

onplaying runs.

Output
=

Sync UI

Hide loader.

Browser Support

The playing event and onplaying handler are supported wherever HTML5 audio and video are supported — all modern browsers.

Media Events · Fully supported

Universal onplaying support

All major browsers fire playing on audio and video elements.

99% Browser support
Google Chrome Fully supported
Full support
Mozilla Firefox Fully supported
Full support
Apple Safari Fully supported
Full support
Microsoft Edge Fully supported
Full support
Internet Explorer IE 9+ (media)
Full support
Opera Fully supported
Full support
onplaying attribute 99% supported

Bottom line: Reliable for buffering and playback-sync UX in all modern browsers.

💡 Best Practices

✅ Do

  • Pair onplaying with onwaiting
  • Use onplaying to hide loading spinners
  • Use addEventListener("playing", …) in production
  • Start time-synced animations on playing
  • Understand the difference from onplay

❌ Don’t

  • Confuse playing with play
  • Use onplaying for “user clicked play” only
  • Put onplaying on non-media elements
  • Assume playing means media ended
  • Block UI without accessible status text

Conclusion

The 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.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about onplaying

Bookmark these before handling media playback events.

5
Core concepts
🔄 02

After play

Later than onplay.

Order
🔎 03

+ waiting

Buffer pair.

Pattern
04

Hide spinner

Classic UX.

UX
05

Not ended

Mid-stream.

Gotcha

❓ Frequently Asked Questions

It runs JavaScript when the playing event fires — when media is actively outputting after buffering or resume.
No. onplay fires when playback is requested. onplaying fires when media is actually playing — often slightly later.
<audio> and <video> — HTML5 media elements only.
When you need to know media is truly rendering — hide spinners after buffering, start synced animations, confirm audible/video output.
media.addEventListener("playing", handler) is preferred for production code.
Yes in all browsers with HTML5 audio and video support.

Sync UI to real playback

Practice the onplaying attribute with audio and buffering examples in the Try It editor.

Try onplaying demo →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful