Silvermine VideoJS Chromecast Plugin
repository·master·Indexed 41 days ago
https://github.com/silvermine/videojs-chromecastA plugin for video.js (v6+) that enables Chromecast casting functionality by adding a cast button to the video player's control bar. Version 1.5.0 provides integration via NPM or script tags, support for custom receiver app IDs, and a ChromecastSessionManager for controlling remote playback and monitoring connection states.
What's inside videojs-chromecast
- The Silvermine VideoJS Chromecast Plugin is an extension for video.js (versions 6 and above). It adds a dedicated Chromecast button to the video.js control bar, allowing users to cast video content to a Chromecast device.
Configure the Chromecast plugin for Video.js
masterTo use the plugin, you must configure the Video.js player.
Critical Requirement: You must set the
techOrderoption so that'chromecast'is the first Tech in the list.Minimum Required Configuration
var options = { controls: true, techOrder: [ 'chromecast', 'html5' ], plugins: { chromecast: {} } }; videojs(document.getElementById('myVideoElement'), options);Alternatively, you can initialize the plugin manually after creating the player:
var player = videojs(document.getElementById('myVideoElement')); player.chromecast();Initialize preloadWebComponents for jQuery compatibility
masterIf your project uses jQuery, you should set
preloadWebComponents: true.This prevents race conditions where the Chromecast framework loads the
webcomponents.jspolyfill dynamically, which can break jQuery's event bubbling. When enabled, the plugin loadswebcomponents-lite.jssynchronously.Note: If using this option, load the plugin before jQuery, and load the Chromecast framework after the plugin.
Via NPM
require('@silvermine/videojs-chromecast')(videojs, { preloadWebComponents: true });Via
<script><script> window.SILVERMINE_VIDEOJS_CHROMECAST_CONFIG = { preloadWebComponents: true, }; </script> <script src="path/to/silvermine-videojs-chromecast.js"></script>Install and integrate @silvermine/videojs-chromecast
masterYou can integrate the plugin using two primary methods:
1. Using the NPM module (Recommended for modern workflows)
Install the package via npm:
npm install @silvermine/videojs-chromecastThen, register it with Video.js using a module loader (like Webpack or Browserify):
var videojs = require('video.js'); require('@silvermine/videojs-chromecast')(videojs);Note: The source uses ES6+. If you need ES5 compatibility, require the pre-transpiled file instead:
require('@silvermine/videojs-chromecast/dist/silvermine-videojs-chromecast.js').2. Using pre-built assets (Direct
<script>tags)Include the plugin after Video.js, and ensure the Chromecast framework is included after the plugin:
<script src="https://unpkg.com/video.js@6.1.0/dist/video.js"></script> <script src="./dist/silvermine-videojs-chromecast.min.js"></script> <script type="text/javascript" src="https://www.gstatic.com/cv/js/sender/v1/cast_sender.js?loadCastFramework=1"></script>var videojs = require('video.js'); // Initialize the Chromecast plugin require('@silvermine/videojs-chromecast')(videojs);Customize Chromecast styles with SCSS
masterIf using SCSS, import the plugin's source file:
@import "path/to/node_modules/@silvermine/videojs-chromecast/src/scss/videojs-chromecast";You can override the following variables:
$icon-chromecast--default: Path to default state icon. (Default:"images/ic_cast_white_24dp.png")$icon-chromecast--hover: Path to hover state icon. (Default:"images/ic_cast_white_24dp.png")$icon-chromecast-casting: Path to icon when casting. (Default:"images/ic_cast_connected_white_24dp.png")$icon-chromecast-casting--hover: Path to icon when casting + hover. (Default:"images/ic_cast_connected_white_24dp.png")$chromecast-icon-size: Icon width/height. (Default:12px)$chromecast-title-font-size: Title font size. (Default:22px)$chromecast-subtitle-font-size: Subtitle font size. (Default:18px)$chromecast-poster-width: Poster width. (Default:100px)$chromecast-poster-max-height: Poster max height. (Default:180px)
Important: If you override icon paths, ensure the images are accessible relative to the compiled CSS file. By default, the plugin expects images to be in an
./images/folder relative to the CSS location.Configure chromecast tech options
masterThese functions allow you to pass dynamic data to the Chromecast receiver during a session.
Option Type Description chromecast.requestTitleFnfunctionReceives sourceobject; returns astringfor the UI title.chromecast.requestSubtitleFnfunctionReceives sourceobject; returns astringfor the UI subtitle.chromecast.requestCustomDataFnfunctionReceives sourceobject; returns anobjectfor custom receiver data.chromecast.modifyLoadRequestFnfunctionReceives LoadRequestobject; returns modifiedLoadRequest. Useful for HLS/fmp4 support.var options = { techOrder: [ 'chromecast', 'html5' ], chromecast: { requestTitleFn: function(source) { return 'My Video Title'; }, modifyLoadRequestFn: function (loadRequest) { loadRequest.media.hlsSegmentFormat = 'fmp4'; return loadRequest; } }, plugins: { chromecast: {} } };Configure plugin.chromecast options
masterThese options control the behavior of the Chromecast plugin UI and connection.
Option Type Default Description plugins.chromecast.receiverAppIDstringdefault ID The ID of a custom Chromecast receiver app. plugins.chromecast.addButtonToControlBarbooleantrueWhether to automatically add the Chromecast button to the control bar. plugins.chromecast.buttonPositionIndexnumberlast child Zero-based index for the button in the control bar. Negative values count from the end. plugins.chromecast.addCastLabelToButtonbooleanfalseIf true, displays the text "Cast" alongside the icon.Register the Chromecast Tech with Video.js
masterThe
chromecastTech is a Video.js Tech implementation that enables playback on Google Chromecast devices. To use it, you must pass thevideojsinstance to the module export, which registers thechromecasttech globally. Once registered, Video.js will automatically select this tech ifChromecastTech.canPlaySource()returns true (i.e., a Chromecast is connected).const videojs = require('video.js'); const registerChromecastTech = require('./path/to/ChromecastTech'); // Register the tech registerChromecastTech(videojs); // Now Video.js can use the 'chromecast' tech automatically const player = videojs('my-player');Use the `addCastLabelToButton` option for the Chromecast button
masterWhen initializing the
ChromecastButton, you can pass theaddCastLabelToButtonoption.- If
true: The button will include a text label (e.g., "Cast" or "Disconnect Cast") and use the CSS classvjs-chromecast-button-lg. - If
false(default): The button behaves as a standard icon button with the control text "Open Chromecast menu".
- If
Configure ChromecastTech via initialization options
masterWhen initializing the
chromecastTech, you can provide several callback functions in theoptionsobject to customize how media metadata and load requests are handled. These functions allow you to inject custom logic for titles, subtitles, custom data, and request modification before the media is sent to the Chromecast device.Available option keys:
requestTitleFn: A function that receives thesourceobject and returns a string for the media title.requestSubtitleFn: A function that receives thesourceobject and returns a string for the media subtitle.requestCustomDataFn: A function that receives thesourceobject and returns an object forchrome.cast.media.LoadRequest.customData.modifyLoadRequestFn: A function that receives thechrome.cast.media.LoadRequestobject and must return a modified (or original)LoadRequestobject.startTime: A number representing the initial playback position in seconds.source: The media source object.muted: A boolean indicating if the player should start muted.
// Example of providing custom logic via options const options = { requestTitleFn: (source) => "My Custom Title", requestSubtitleFn: (source) => "My Custom Subtitle", requestCustomDataFn: (source) => ({ myKey: 'myValue' }), modifyLoadRequestFn: (request) => { // Modify the Google Cast LoadRequest object directly request.autoplay = true; return request; }, startTime: 10, source: { src: 'http://example.com/video.mp4', type: 'video/mp4' } };Configure the videojs-chromecast plugin options
masterWhen initializing the plugin, you can pass a
userOptsobject to configure its behavior.Currently, the following option is supported:
Option Type Default Description preloadWebComponentsbooleanfalseIf set to true, the plugin will explicitly callpreloadWebComponents()to prepare necessary web components.// Example configuration videojsChromecast(videojs, { preloadWebComponents: true });Configure the position of the Chromecast button in the control bar
masterThe
chromecastButtonis a Video.js component that is automatically added to the player's control bar once the plugin is enabled. If you want to control the specific order of buttons in the control bar, you can include'chromecastButton'in thecontrolBar.childrenarray when initializing the player.videojs('playerID', { controlBar: { children: [ 'playToggle', 'progressControl', 'volumePanel', 'fullscreenToggle', 'chromecastButton', ], }, });