dash.js
repository·development·Indexed 26 days ago
https://github.com/dash-industry-forum/dash.jsA reference JavaScript client implementation for the playback of MPEG DASH content in compliant web browsers. It utilizes Media Source Extensions (MSE) and Encrypted Media Extensions (EME) for playback and content protection. The library includes a MediaPlayer class and optional components such as a reusable ControlBar for UI management and integration plugins like videojs-contrib-dash for Video.js.
What's inside dash.js
- dash.js is a JavaScript-based implementation for playing MPEG DASH content in browser environments. It relies on the Media Source Extensions (MSE) and Encrypted Media Extensions (EME) for playback and content protection.
Integrate Dash.js with Video.js using videojs-contrib-dash
developmentThe
videojs-contrib-dashplugin provides an integration between Video.js and Dash.js.By using this plugin, you can:
- Add MPEG-DASH support to Video.js in browsers that support Media Source Extensions (MSE).
- Access all Video.js plugins while playing DASH content.
- Use custom Video.js skinning.
- Enable fallback support to other video formats (HLS, MP4, or WebM) through other technologies like HTML5, Flash, or YouTube.
Quickstart with dash.js
developmentTo use dash.js in a web application, include the
dash.all.min.jslibrary via CDN, select a<video>element, and usedashjs.MediaPlayer().create()to initialize the player with your MPEG DASH manifest URL. Theinitializemethod takes the video element, the manifest URL, and a boolean for autoplay.<!doctype html> <html lang="en"> <head> <title>dash.js Rocks</title> <style> video { width: 640px; height: 360px; } </style> </head> <body> <div> <video id="videoPlayer" controls></video> </div> <script src="https://cdn.dashjs.org/latest/modern/umd/dash.all.min.js"></script> <script> (function () { var url = "https://dash.akamaized.net/envivio/EnvivioDash3/manifest.mpd"; var player = dashjs.MediaPlayer().create(); player.initialize(document.querySelector("#videoPlayer"), url, true); })(); </script> </body> </html>Initialize and use the ControlBar
developmentTo integrate the ControlBar, import the
ControlBarclass as an ES module, initialize yourdashjs.MediaPlayerinstance, and then pass both the player and the video element to theControlBarconstructor. Finally, call.init()with your wrapper element and.enable()to make it interactive.import { ControlBar } from 'path/to/contrib/controlbar/ControlBar.js'; const player = dashjs.MediaPlayer().create(); const video = document.getElementById('video-element'); player.initialize(video, url, autoPlay); const controlbar = new ControlBar(player, video); controlbar.init(document.getElementById('video-wrapper')); controlbar.enable();Set up ControlBar.js with Dash.js
developmentTo use the Akamai ControlBar with a Dash.js player, follow these three steps:
- Import Assets: Include the
controlbar.cssandControlBar.jsfiles in your HTML. - Add HTML Structure: Insert the required
video-controllerHTML snippet into your DOM. Ensure that buttons requiring specific layouts have thecontrol-icon-layoutCSS class applied to avoid layout issues. - Initialize in JavaScript: Attach the video to the Dash.js player, instantiate the
ControlBarwith the player instance, and call.initialize().
<!-- 1. Import CSS and JS --> <link rel="stylesheet" href="../../contrib/akamai/controlbar/controlbar.css"> <script src="../../contrib/akamai/controlbar/ControlBar.js"></script> <!-- 2. Add HTML Snippet --> <div id="videoController" class="video-controller unselectable"> <div id="playPauseBtn" class="btn-play-pause" title="Play/Pause"> <span id="iconPlayPause" class="icon-play"></span> </div> <span id="videoTime" class="time-display">00:00:00</span> <div id="fullscreenBtn" class="btn-fullscreen control-icon-layout" title="Fullscreen"> <span class="icon-fullscreen-enter"></span> </div> <div id="bitrateListBtn" class="control-icon-layout" title="Bitrate List"> <span class="icon-bitrate"></span> </div> <input type="range" id="volumebar" class="volumebar" value="1" min="0" max="1" step=".01"/> <div id="muteBtn" class="btn-mute control-icon-layout" title="Mute"> <span id="iconMute" class="icon-mute-off"></span> </div> <div id="trackSwitchBtn" class="control-icon-layout" title="A/V Tracks"> <span class="icon-tracks"></span> </div> <div id="captionBtn" class="btn-caption control-icon-layout" title="Closed Caption"> <span class="icon-caption"></span> </div> <span id="videoDuration" class="duration-display">00:00:00</span> <div class="seekContainer"> <input type="range" id="seekbar" value="0" class="seekbar" min="0" step="0.01"/> </div> </div> <script> // 3. Create ControlBar.js player.attachView(video); var controlbar = new ControlBar(player); // Player is instance of Dash.js MediaPlayer; controlbar.initialize(); </script>- Import Assets: Include the
Initialize a MediaPlayer in Dash.js v2.0
developmentIn Dash.js v2.0, you create a player by calling
dashjs.MediaPlayer().create()and then initializing it with a video element and a manifest URL.var url = "https://dash.akamaized.net/envivio/Envivio-dash2/manifest.mpd"; var element = document.querySelector("#selector"); var player = dashjs.MediaPlayer().create(); player.initialize(element, url, true);var url = "https://dash.akamaized.net/envivio/Envivio-dash2/manifest.mpd"; var element = document.querySelector("#selector") var player = dashjs.MediaPlayer().create(); player.initialize(element, url, true);Instantiate multiple ControlBar instances
developmentTo use multiple players with separate control bars on the same page, append a unique suffix to every element ID in your HTML snippet. When calling the
initializemethod, pass that same suffix as a string argument.<!-- HTML: Append suffix (e.g., '_1') to all IDs --> <div id="videoController_1" class="video-controller unselectable"> <div id="playPauseBtn_1" class="btn-play-pause" title="Play/Pause"> <span id="iconPlayPause_1" class="icon-play"></span> </div> <!-- ... other elements with _1 suffix ... --> </div> <script> // JS: Pass the suffix to initialize var controlbar = new ControlBar(player); controlbar.initialize('_1'); </script>Build dash.js module samples
developmentEach sample directory in the
samples/modules/path is a standalone package. You can build the sample using the provided npm script. The build output will be generated inout/out.js.npm run buildConsume dash.js with WebPack and TypeScript
developmentYou can consume dash.js modules using WebPack with TypeScript. An implementation example is available in thetypescriptdirectory.Install and set up the dash.js ControlBar
developmentThe dash.js ControlBar is a reusable video control bar that generates its own DOM structure. To use it, you must ensure
dash.jsis loaded and include the Bootstrap Icons CSS for icon rendering.Prerequisites
- dash.js must be loaded (the global
dashjsobject must be available). - Bootstrap Icons CSS must be included:
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1/font/bootstrap-icons.min.css" rel="stylesheet">
Setup Steps
- Include the ControlBar CSS:
<link rel="stylesheet" href="path/to/contrib/controlbar/controlbar.css"> - Provide a wrapper element in your HTML. This element must have
position: relativein its CSS to contain the control bar correctly:<div id="video-wrapper" style="position: relative;"> <video id="video-element"></video> </div>
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1/font/bootstrap-icons.min.css" rel="stylesheet"> <link rel="stylesheet" href="path/to/contrib/controlbar/controlbar.css"> <div id="video-wrapper" style="position: relative;"> <video id="video-element"></video> </div>- dash.js must be loaded (the global
Manage the ControlBar lifecycle
developmentWhen working with dynamic streams, follow this typical lifecycle to ensure the control bar state remains synchronized with the player:
- Create and Initialize: Instantiate the bar and call
.init(). - Stream Ready: Call
.enable()when the stream is ready. - New Stream Loading: Before loading a new source, call
.reset()and.disable(). - Post-Load: After the new source is loaded, call
.syncMuteState()to ensure the player's mute/volume settings match the UI, then call.enable()again. - Cleanup: Call
.destroy()when the player or component is being removed.
// Create const cb = new ControlBar(player, video); cb.init('#video-wrapper'); cb.disable(); // On stream initialized cb.enable(); // Before loading a new stream cb.reset(); cb.disable(); // After loading cb.syncMuteState(); // On stream initialized again cb.enable(); // Cleanup cb.destroy();- Create and Initialize: Instantiate the bar and call
Setup the simple-express-server sample
developmentThe
simple-express-serveris a minimal Express server used by dash.js samples. To set it up, ensure you have Node.js (and npm) installed, then install the dependencies.npm install