Skip to main content
The Vidinfra Player SDK is a lightweight, type safe JavaScript library for controlling embedded video players inside iframes. It uses the 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:
This method allows you to use the SDK without needing to install it locally, making it easy to integrate into static or non-Node.js projects.

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 the Player 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 embedding
  • options - 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.
Pause video playback.
Toggle between play and pause states.
Seek to a specific time in seconds.
Set playback speed (e.g., 0.5, 1.0, 2.0).
Get current playback rate.
Get current playback position in seconds.
Get total video duration in seconds.
Check if video is currently paused.

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).
Get current volume level.
Mute audio.
Unmute audio.
Set muted state.
Get current muted state.

Display Methods

These methods manage the visual behavior of the player, including fullscreen and control visibility. Toggle fullscreen mode.
Enter fullscreen mode.
Exit fullscreen mode.
Show player controls.
Hide player controls.
Set controls visibility.

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.
WatermarkConfig:

Utility Methods

These methods help you interact with the player’s internal state and configuration. Check if player is ready to receive commands.
Returns a promise that resolves when the player is ready.
Get the iframe element.
Get the wrapper element (only available for embedded players).
Get the iframe source URL.
Update player options (only available for embedded players).
Clean up event listeners and remove the player.

Event Methods

These methods allow you to listen for events like play, pause, and custom player events. Register an event listener.
Remove an event listener.
Register a one-time event listener.

Events

The player emits the following events:

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 postMessage API
  • Fullscreen API support varies by browser

TypeScript

Full TypeScript definitions are included with the package: