Course contentsShow
← Web Development
Lesson 741 of 2,98213. DOM EventsFree lesson

Media Events: play, pause, ended, timeupdate

Control and respond to audio/video playback with media element events.

Media Events: play, pause, ended, timeupdate

You'll learn how to listen for and respond to audio and video playback events in your web applications.

What Are Media Events?

Media events are fired by <audio> and <video> elements during playback, allowing you to respond to user interactions like playing or pausing, or automatic changes like the video ending. Think of them like status updates from a streaming service — the player tells you "I just started," "I'm paused," or "I've finished."

Core Media Events

play

Fires when playback begins. This happens when the user clicks play or when you call .play() programmatically.

pause

Fires when playback is paused. Triggered by user action or calling .pause().

ended

Fires when the media reaches its end naturally. Perfect for showing "replay" buttons or loading the next video.

timeupdate

Fires repeatedly as the media plays (typically several times per second). Use this to update progress bars or display the current timestamp. This is your "heartbeat" event for tracking where the user is in the media.

Common Usage Pattern

const video = document.querySelector('video');

video.addEventListener('play', function() {
  console.log('Video started playing');
});

video.addEventListener('pause', function() {
  console.log('Video paused');
});

video.addEventListener('ended', function() {
  console.log('Video finished');
});

video.addEventListener('timeupdate', function() {
  console.log('Current time:', video.currentTime);
});

These events work identically for both <audio> and <video> elements, making it easy to build custom media controls or track user engagement.

Key Takeaway: Media events let you monitor and respond to audio/video playback states, with play, pause, and ended tracking major state changes, while timeupdate provides continuous progress updates.