End-to-end walkthrough
Build an MKPlayer integration from a blank HTML file to a working player that loads DRM-protected content, displays subtitles, and handles errors. Complete the sections in order because each one builds on the previous result.
By the end you will have a self-contained page you can adapt for your own project.
What this covers
-
Installing MKPlayer and wiring up the HTML
-
Initialising the player with a full configuration
-
Loading DRM-protected content with Widevine, PlayReady, and FairPlay
-
Enabling subtitles and styling them at runtime
-
Subscribing to events and surfacing errors to the user
-
Cleaning up the player correctly when done
Prerequisites
-
npm available in your project
-
A player license key from MediaKind
-
Your deployment domain added to the MediaKind license allowlist
-
A Widevine and/or PlayReady license server URL for your content
The HTML page
Start with a minimal HTML page. Include the MKPlayer script and stylesheet in <head>, and add a container <div> and a few controls in <body>:
The status paragraph is where you will surface error messages to the user. The actual player logic lives in player.js.
Step 1: Initialise the player
In player.js, wait for the DOM to be ready, then create an MKPlayer instance.
The player configuration sets your license key, enables the built-in UI, and registers event handlers inline. Subscribing to Error and Playing here ensures you catch events that fire early in the player lifecycle, before you have a chance to call player.on() after construction.
player.js
Unmuted autoplay is blocked by most browsers unless the user has previously interacted with the page. If you need autoplay, set muted: true. The user can then unmute using the player controls.
Step 2: Load DRM-protected content
Define loadContent() to build a source configuration and call player.load(). The drm block covers Widevine and PlayReady for Chrome, Firefox, and Edge, plus FairPlay for Safari on Apple devices. The player selects the first key system supported by the current browser.
Step 3: Enable subtitles
Once the source has loaded, list the available subtitle tracks and enable one by default. You can also let the player select a preferred language by setting subtitleLanguage in the playback configuration. This example selects the track explicitly so you can use its ID in the next step.
Step 4: Add remaining event handling
Add event listeners after the player is created to handle the remaining playback lifecycle. This example registers them dynamically with player.on() instead of adding them to the configuration:
Call attachEvents(player) immediately after creating the player instance, before loadContent().
Step 5: Clean up
When the page unloads, or when you are done with the player, unload the source and destroy the instance to release all held resources:
Do not call any player API methods after destroy(). The player will throw a PLAYER_API_NOT_AVAILABLE error.
Complete player.js
Putting it all together:
player.js
What to replace
Before running this, replace the following placeholder values with your own:
Common issues at this stage
The player throws PLAYER_SETUP_MISSING_LICENSE_ALLOWLIST.
Your domain is not on the MediaKind license allowlist. Contact MediaKind to add it before testing.
The player throws PLAYER_DRM_FAILED_LICENSE_REQUEST.
The license server rejected the request. Check that your LA_URL is correct, that any required auth headers are present, and that the token has not expired.
Subtitles do not appear after player.subtitles.enable().
Check that enableSubtitleOverlay is not set to false in your player configuration and that ui is set to false. The built-in subtitle overlay is disabled when ui is enabled. If you are using ui: true, the UI layer handles subtitle rendering.
Safari does not play back the stream.
Ensure you have placed serviceWorker.js at the root of your domain and set tweaks: { native_hls_parsing: true } in your player config. See Installation and setup for details.