Core Engine

MP4 Engine

The core headless engine for progressive HTML5 video.

MP4 Engine (Mp4Player)

The Mp4Player class manages native HTML5 video playback for progressive files (MP4, WebM, Ogg). It implements the standard PlayerControls interface to ensure consistency with the HLS and YouTube engines.

Basic Usage

import { Mp4Player } from "@playerkit/core";

const videoElement = document.querySelector("video");

const player = new Mp4Player({
  video: videoElement,
  src: "https://example.com/video.mp4",
});

player.play();

Options

OptionTypeDescription
videoHTMLVideoElementRequired. The native video element to bind to
srcstringRequired. The progressive video URL
rootHTMLElementElement to use for fullscreen API
autoPlaybooleanStart playback automatically
startTimenumberTime to seek to on load
tokenFetcherTokenFetcherAsync function for protected MP4 URLs
tokenRefresherTokenRefresherAsync background polling function

The live config option is not available for Mp4Player as progressive files are always Video-on-Demand (VOD).


Why use Mp4Player instead of vanilla HTMLVideoElement?

You might wonder why you need a class to wrap a native <video> tag. Using Mp4Player provides:

  1. API Consistency: If your app switches between HLS and MP4, you can use the exact same subscribe and PlayerControls logic without writing if (isHls) branches.
  2. Token Auth: It supports the same tokenFetcher and tokenRefresher background polling logic as the HLS engine.
  3. Normalized State: It calculates bufferedPercentage and formats the state into a clean PlayerSnapshot.
  4. Security: It hooks into the @playerkit/core SecurityManager to block DevTools and context menus if you enable enterprise protections.

Cleanup

// Detaches listeners and clears token refresh timers
player.destroy();

On this page