Web Player SDK
The Vidinfra Player SDK is a lightweight, type-safe JavaScript library designed to control video players inside iframes. It leverages the power of the postMessage API for secure, real-time communication between the parent page and embedded video players. Whether youโre integrating a video player into your app or building custom controls, the Player SDK makes it easy and efficient. This documentation provides an overview of the libraryโs features, installation, usage instructions, and examples to help you get started quickly.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.Controlling an Existing Iframe
To control a player inside an existing iframe, use thePlayer class:
Create an Embedded Player
To automatically create and embed the video player, use the following: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
Playback Methods
play(): Promise<void>: Start video playback.pause(): Promise<void>: Pause video playback.togglePlay(): Promise<void>: Toggle between play and pause states.seek(time: number): Promise<void>: Seek to a specific time in seconds.setPlaybackRate(rate: number): Promise<void>: Set playback speed (e.g., 0.5, 1.0, 2.0).getPlaybackRate(): Promise<number>: Get current playback rate.getCurrentTime(): Promise<number>: Get current playback position in seconds.getDuration(): Promise<number>: Get total video duration in seconds.getPaused(): Promise<boolean>: Check if video is currently paused.
Volume Methods
setVolume(volume: number): Promise<void>: Set volume level (0-1).getVolume(): Promise<number>: Get current volume level.mute(): Promise<void>: Mute audio.unmute(): Promise<void>: Unmute audio.setMuted(muted: boolean): Promise<void>: Set muted state.getMuted(): Promise<boolean>: Get current muted state.
Display Methods
toggleFullscreen(): Promise<void>: Toggle fullscreen mode.requestFullscreen(): Promise<void>: Enter fullscreen mode.exitFullscreen(): Promise<void>: Exit fullscreen mode.showControls(): Promise<void>: Show player controls.hideControls(): Promise<void>: Hide player controls.setControlsVisible(visible: boolean): Promise<void>: Set controls visibility.
Watermark Methods
addWatermark(config: WatermarkConfig): Promise<void>: Add a watermark overlay to the video.
Utility Methods
isReady(): boolean: Check if player is ready to receive commands.whenReady(): Promise<void>: Returns a promise that resolves when the player is ready.getIframe(): HTMLIFrameElement: Get the iframe element.getWrapper(): HTMLElement | null: Get the wrapper element (only available for embedded players).getSrc(): string: Get the iframe source URL.update(options: Partial<PlayerOptions>): void: Update player options (only available for embedded players).destroy(): void: Clean up event listeners and remove the player.
Event Methods
on(event: string, callback: Function): this: Register an event listener.off(event: string, callback?: Function): this: Remove an event listener.once(event: string, callback: Function): this: Register a one-time event listener.
Events
The player emits the following events:ready- Player is ready to receive commandsplay- Playback has startedplaying- Playback is playingpause- Playback has pausedended- Playback has endedtimeupdate- Playback position changedvolumechange- Volume or muted state changedseeking- Seeking startedseeked- Seeking completedwaiting- Waiting for datacanplay- Enough data to playcanplaythrough- Can play through without bufferingloadedmetadata- Metadata loadedloadeddata- Frame data loadeddurationchange- Duration changedratechange- Playback rate changedfullscreenchange- Fullscreen state changederror- 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