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
| Option | Type | Description |
|---|---|---|
video | HTMLVideoElement | Required. The native video element to bind to |
src | string | Required. The progressive video URL |
root | HTMLElement | Element to use for fullscreen API |
autoPlay | boolean | Start playback automatically |
startTime | number | Time to seek to on load |
tokenFetcher | TokenFetcher | Async function for protected MP4 URLs |
tokenRefresher | TokenRefresher | Async 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:
- API Consistency: If your app switches between HLS and MP4, you can use the exact same
subscribeandPlayerControlslogic without writingif (isHls)branches. - Token Auth: It supports the same
tokenFetcherandtokenRefresherbackground polling logic as the HLS engine. - Normalized State: It calculates
bufferedPercentageand formats the state into a cleanPlayerSnapshot. - Security: It hooks into the
@playerkit/coreSecurityManagerto block DevTools and context menus if you enable enterprise protections.
Cleanup
// Detaches listeners and clears token refresh timers
player.destroy();