postMessage API for secure, real time communication, making custom player integration simple and efficient.
Key Features
- Unified API: One class manages both controlling existing iframes and embedding new players.
- Type-safe: Full TypeScript support with comprehensive type definitions for a reliable development - experience.
- Framework-agnostic: Works seamlessly with any JavaScript framework or plain JavaScript.
- Secure: Built-in origin validation for postMessage communication to ensure safe interactions.
- Event-driven: Listen to real-time player events such as play, pause, and time updates.
- Lightweight: The SDK is approximately 15KB when minified, making it efficient and easy to use.
Installation
NPM Installation
To install the Vidinfra Player SDK via npm, run the following command:CDN Installation
Alternatively, you can load the SDK via a CDN:Quick Start
The Player SDK allows you to control existing iframe players and create embedded players on the fly. Below are the basic instructions for both use cases.Control an Existing Iframe
If you already have an iframe on your page and want to control the video player inside it, you can easily use thePlayer class. With just a few lines of code, you can target an existing iframe, wait for it to be ready, and start playing the video.
Creating an Embedded Player
If you want to embed a video player directly into your page, Vidinfra can automatically create and inject an iframe for you. You can configure the player with your desired settings like autoplay, volume, and aspect ratio.To learn how to copy a video ID, check the Quickstart Guide in the Vidinfra section.
Usage
Controlling an Existing Iframe
If you already have an iframe in your HTML, you can control it by passing the iframe element or a CSS selector.Javascript
CDN
Creating an Embedded Player
You can dynamically create an embedded player by specifying a target element for the player:Javascript
CDN
API Reference
Constructor
Parameters
element- An iframe element, CSS selector for an iframe, or a container element for embeddingoptions- Configuration options for creating an embedded player (required when creating an embed)
PlayerOptions
The PlayerOptions defines all the customizable settings for configuring a Vidinfra player. Here, you specify key details about the video and player behavior. You can set the videoโs library ID, video ID, and the playerโs size, playback options, and more.Playback Methods
These methods control the basic playback behavior of the video. They include actions like starting, pausing, seeking, and changing playback speed. Start video playback.Volume Methods
These methods manage the audio settings for the player. You can adjust the volume, mute, or check the current audio state. Set volume level (0-1).Display Methods
These methods manage the visual behavior of the player, including fullscreen and control visibility. Toggle fullscreen mode.Watermark Methods
These methods add a watermark to your video, which can be customized with text and styling options. Add a watermark overlay to the video.Utility Methods
These methods help you interact with the playerโs internal state and configuration. Check if player is ready to receive commands.Event Methods
These methods allow you to listen for events like play, pause, and custom player events. Register an event listener.Events
The player emits the following events:| Event | Description |
|---|---|
ready | Player is ready to receive commands |
play | Playback has started |
playing | Playback is playing |
pause | Playback has paused |
ended | Playback has ended |
timeupdate | Playback position changed |
volumechange | Volume or muted state changed |
seeking | Seeking started |
seeked | Seeking completed |
waiting | Waiting for data |
canplay | Enough data to play |
canplaythrough | Can play through without buffering |
loadedmetadata | Metadata loaded |
loadeddata | Frame data loaded |
durationchange | Duration changed |
ratechange | Playback rate changed |
fullscreenchange | Fullscreen state changed |
error | Playback error occurred |
Examples
Basic Playback Control
Volume Control
Creating and Controlling an Embed
TypeScript Example
Security
The Player automatically validates the origin of postMessage communications based on the iframeโs source URL. For iframe-side implementations, you should also validate the origin of incoming messages:Browser Support
- Modern browsers with ES6+ support
- Requires
postMessageAPI - Fullscreen API support varies by browser