Listen to player events
Track and respond to FastPix player events, such as play, pause, seek, buffering, and completion, to monitor playback and improve interactivity.
By listening to events, you can track key moments like when a video starts, pauses, ends, or encounters an error. This capability is particularly useful for triggering additional actions or analytics during playback. In this guide, we’ll walk through how you can use this feature, followed by two practical examples.
Why listen to video events?
The HTML <video> element triggers various media events throughout its lifecycle, such as:
- play: When the video starts playing.
- pause: When the video is paused.
- ended: Triggered when the video playback is complete.
- timeupdate: Periodically trigerred as the video plays, giving updates on the current time.
- error: Fired when the video encounters a playback issue.
By having fastpix-player listen to these events, you gain access to detailed control over video playback, enabling you to:
- Handle errors gracefully by displaying user-friendly messages or troubleshooting advice.
- Create engaging, interactive experiences by responding to specific moments in the video.
How to make the player listen to events
To listen for video events in the player, you can use the addEventListener() method. This method allows you to listen for events on the video element and execute custom JavaScript functions when these events are triggered.
Here’s a basic example of setting up event listeners for play, pause, and ended events:
In this example, the querySelector() method is used to locate the underlying <video> element inside the fastpix-player element, enabling event listeners to be attached.
FastPix custom events
Besides the native HTML media events above, the player fires its own events for tracks, quality, chapters, and playlists. Listen for them the same way, with addEventListener.
Track events
Quality events
Chapter and playlist events
To build a custom track UI around these events, see Build a custom track switcher UI. For the playlist events, see Add a playlist.
Control playback from JavaScript
The player exposes methods for controlling playback from your own code, which is useful for custom controls, Shorts-style UIs, or framework integrations.
For relative seeking from your own controls, use player.seekForward(seconds) and player.seekBackward(seconds). See Build a custom track switcher UI for a full custom-controls example.
Example 1: Tracking playback progress
The timeupdate event is useful for tracking video progress in real-time, such as updating a custom progress bar or triggering certain actions at specific times. Here’s how you can set up a listener to update the playback progress dynamically:
Example 2: Error handling with the error event
Playback errors can occur due to network issues, unsupported formats, or other reasons. By listening to the error event, you can display user-friendly error messages and guide users through potential solutions:
Explanation:
errorevent: This event fires when a video playback issue arises, such as an unsupported format or network error.- Custom error handling: The
errorobject contains acodeproperty that helps identify the type of problem. This example uses the code to display relevant error messages, improving the user experience by making issues clearer.