video.js plugin for casting to chromecast
npm install @silvermine/videojs-chromecast




A plugin for videojs versions 6+ that adds a button to the control
bar which will cast videos to a Chromecast.
The @silvermine/videojs-chromecast plugin includes 3 types of assets: JavaScript, CSS,
and images.
You can either build the plugin locally and use the assets that are output from the build
process directly, or you can install the plugin as an NPM module, include the
JavaScript and SCSS source in your project using a Common-JS module loader and SASS build
process, and copy the images from the image source folder to your project.
Note that regardless of whether you are using this plugin via the pre-built JS or as a
module, the Chromecast framework will need to be included after the plugin. For example:
``html`
1. Either clone this repository or install the @silvermine/videojs-chromecast modulenpm install @silvermine/videojs-chromecast
using .@silvermine/videojs-chromecast
2. Ensure that 's devDependencies are installed bynpm install
running from within the videojs-chromecast folder.grunt build
3. Run to build and copy the JavaScript, CSS and image files to thevideojs-chromecast/dist
folder.dist
4. Copy the plugin's files from the folder into your project as needed.dist/images
5. Ensure that the images in the folder are accessible at ./images/,https://example.com/plugins/silvermine-videojs-chromecast.css
relative to where the plugin's CSS is located. If, for example, your CSS is located
at , then thehttps://example.com/plugins/images/
plugin's images should be located at .
6. Follow the steps in the "Configuration" section below.
Note: when adding the plugin's JavaScript to your web page, include the
silvermine-videojs-chromecast.min.js JavaScript file in your HTML after loadingwindow.videojs
Video.js. The plugin's built JavaScript file expects there to be a reference to Video.js
at and will throw an error if it does not exist.
* preloadWebComponents (default: false) - The Chromecast framework relies onwebcomponents.js
the polyfill when a browser does not havedocument.registerElement
in order to create the customwebcomponents.js
component (which is not used by this plugin). If you are using jQuery, this polyfill
must be loaded and initialized before jQuery is initialized. Unfortunately, the
Chromecast framework loads the polyfill via a dynamically created
`
Once the plugin has been loaded and registered, configure it and add it to your Video.js
player using Video.js' plugin configuration option (see the section under the heading
"Setting up a Plugin" on [Video.js' plugin documentation page][videojs-docs].
**Important: In addition to defining plugin configuration, you are required to define the
player's techOrder option, setting 'chromecast' as the first Tech in the list.** Below
is an example of the minimum required configuration for the Chromecast plugin to function:
`js
var options;
options = {
controls: true,
techOrder: [ 'chromecast', 'html5' ], // You may have more Tech, such as Flash or HLS
plugins: {
chromecast: {}
}
};
videojs(document.getElementById('myVideoElement'), options);
`
Please note that even if you choose not to use any of the configuration options, you must
either provide a chromecast entry in the plugins option for Video.js to initialize the
plugin for you:
`js`
options = {
plugins: {
chromecast: {}
}
};
or you must initialize the plugin manually:
`js
var player = videojs(document.getElementById('myVideoElement'));
player.chromecast(); // initializes the Chromecast plugin
`
#### Configuration options
##### Plugin configuration
* plugins.chromecast.receiverAppID - the string ID of a custom [Chromecast
receiver app][cast-receiver] to use. Defaults to the [default Media Receiver
ID][def-cast-id].
* plugins.chromecast.addButtonToControlBar - a boolean flag that tells thetrue
plugin whether or not it should automatically add the Chromecast button to the
Video.js player's control bar component. Defaults to .plugins.chromecast.buttonPositionIndex
* - a zero-based number specifying theaddButtonToControlBar
index of the Chromecast button among the control bar's child components (if
is set to true). By default the Chromecast Button is addedplugins.chromecast.addCastLabelToButton
as the last child of the control bar. A value less than 0 puts the button at the
specified position from the end of the control bar. Note that it's likely not all
child components of the control bar are visible.
* (default: false) - by default, theaddCastLabelToButton
Chromecast button component will display only an icon. Setting true
to will display a label titled "Cast" alongside the default icon.
##### Chromecast Tech configuration
* chromecast.requestTitleFn - a function that this plugin calls when it needs a
string that will be the title shown in the UI that is shown when a Chromecast session
is active and connected. When the this plugin calls the requestTitleFn, it passessource
it the [current object][player-source] and expects a string in return. Ifchromecast.requestSubtitleFn
nothing is returned or if this option is not defined, no title will be shown.
* - a function that this plugin calls when it needsrequestSubtitleFn
a string that will be the sub-title shown in the UI that is shown when a Chromecast
session is active and connected. When the this plugin calls the ,source
it passes it the [current object][player-source] and expects a string inchromecast.requestCustomDataFn
return. If nothing is returned or if this option is not defined, no sub-title will be
shown.
* - a function that this plugin calls when itrequestCustomDataFn
needs an object that contains custom information necessary for a Chromecast receiver
app when a session is active and connected. When the this plugin calls the
, it passes it the [current source object][player-source] andchromecast.modifyLoadRequestFn
expects an object in return. If nothing is returned or if this option is not defined,
no custom data will be sent. This option is intended to be used with a [custom
receiver][custom-receiver] application to extend its default capabilities.
* - a function that this plugin calls before doing
the request to [load media][chromecast-load-media]. The function gets called with
the [LoadRequest][chromecast-load-request] object as argument and expects it in
return.
Here is an example configuration object that makes full use of all required and optional
configuration:
`js
var titles, subtitles, customData, options;
titles = {
'https://example.com/videos/video-1.mp4': 'Example Title',
'https://example.com/videos/video-2.mp4': 'Example Title2',
};
subtitles = {
'https://example.com/videos/video-1.mp4': 'Subtitle',
'https://example.com/videos/video-2.mp4': 'Subtitle2',
};
customData = {
'https://example.com/videos/video-1.mp4': { 'customColor': '#0099ee' },
'https://example.com/videos/video-2.mp4': { 'customColor': '#000080' },
};
options = {
// Must specify the 'chromecast' Tech first
techOrder: [ 'chromecast', 'html5' ], // Required
// Configuration for the Chromecast Tech
chromecast: {
requestTitleFn: function(source) { // Not required
return titles[source.url];
},
requestSubtitleFn: function(source) { // Not required
return subtitles[source.url];
},
requestCustomDataFn: function(source) { // Not required
return customData[source.url];
},
modifyLoadRequestFn: function (loadRequest) { // HLS support
loadRequest.media.hlsSegmentFormat = 'fmp4';
loadRequest.media.hlsVideoSegmentFormat = 'fmp4';
return loadRequest;
}
},
plugins: {
chromecast: {
receiverAppID: '1234' // Not required
addButtonToControlBar: false, // Defaults to true
},
}
};
`
##### Localization
The ChromecastButton component has two translated strings: "Open Chromecast menu" and
"Cast".
* The "Open Chromecast menu" string appears in both of the standard places for Button
component accessibility text: inside the .vjs-control-text span and as the
To localize the Chromecast button strings, follow the steps in the [Video.js Languages
tutorial][videojs-translation] to add "Open Chromecast menu" and "Cast" keys to the
map of translation strings.
If you are using a module loader such as Browserify or Webpack, first install
@silvermine/videojs-chromecast using npm install. Then, userequire('@silvermine/videojs-chromecast') to require @silvermine/videojs-chromecastrequire('@silvermine/videojs-chromecast')
into your project's source code. returns avideojs
function that you can use to register the plugin with Video.js by passing in a reference
to :
`js
var videojs = require('video.js');
// Initialize the Chromecast plugin
require('@silvermine/videojs-chromecast')(videojs);
`
Then, follow the steps in the "Configuration" section above.
This plugin's source code uses ES6+ syntax and keywords, such as class and static. If
you need to support browsers that do not support newer JavaScript
syntax, you will need to use a tool like
Babel to transpile and polyfill your code.
Alternatively, you can
require('@silvermine/videojs-chromecast/dist/silvermine-videojs-chromecast.js') to use a
JavaScript file that has already been polyfilled/transpiled down to ES5 compatibility.
If you are using SCSS in your project, you can simply reference the plugin's main SCSS
file in your project's SCSS:
`scss`
@import "path/to/node_modules/@silvermine/videojs-chromecast/src/scss/videojs-chromecast";
Optionally, you can override the SCSS variables that contain the paths to the icon image
files:
* $icon-chromecast--default - the path to the icon image that is displayed when
the Chromecast button is in its normal, default state. Defaults to
"images/ic_cast_white_24dp.png".$icon-chromecast--hover
* - the path to the icon image that is displayed when the"images/ic_cast_white_24dp.png"
user hovers over the Chromecast button when it is in its normal, default state.
Defaults to .$icon-chromecast-casting
* - the path to the icon image that is displayed when"images/ic_cast_connected_white_24dp.png"
the Chromecast button is in the "casting" state (when a Chromecast session is active
and connected). Defaults to .$icon-chromecast-casting--hover
* - the path to the icon image that is displayed"images/ic_cast_connected_white_24dp.png"
when the user hovers over the Chromecast button when it is in the "casting" state
(when a Chromecast session is active and connected). Defaults to
.$chromecast-icon-size
* - the width and height of the icon (the button and icon12px
is a square). Defaults to .$chromecast-title-font-size
* - the font size of the title on the screen that is22px
shown while a Chromecast session is active and connected. Defaults to .$chromecast-subtitle-font-size
* - the font size of the sub-title on the screen18px
that is shown while a Chromecast session is active and connected. Defaults to .$chromecast-poster-width
* - the width of the poster image on the screen that100px
that is shown while a Chromecast session is active and connected. Defaults to
.$chromecast-poster-max-height
* - the maximum height of the poster image on the180px
screen that is shown while a Chromecast session is active and connected. Defaults to
.
#### Images
The plugin's images are located at videojs-chromecast/src/images. If you havesrc/images
not overridden the icon image path variables in the SCSS, then copy the images from the folder to a folder that is accessible at ./images/, relative to where thehttps://example.com/plugins/silvermine-videojs-chromecast.css
plugin's CSS is located. If, for example, your CSS is located at, then the plugin's imageshttps://example.com/plugins/images/
should be located at .
In addition, the ic_cast_white_24dp.png icon image that is used as the default icon forimages
all four button states ("default", "default + hover", "casting", "casting + hover"), the folder contains grey, black, and blue versions of the icons.
* chromecastConnected - Triggers when Chromecast connectedchromecastDisconnected
* - Triggers when Chromecast disconnectedchromecastDevicesAvailable
* - Triggers on state change when Chromecast devices arechromecastDevicesUnavailable
available
* - Triggers on state change when Chromecast devices arechromecastRequested`: Triggers when the user has requested Chromecast playback using
unavailable
*
this plugin's Chromecast button
We genuinely appreciate external contributions. See [our extensive
documentation][contributing] on how to contribute.
This software is released under the MIT license. See the license file for more
details.
[videojs-docs]: http://docs.videojs.com/tutorial-plugins.html
[videojs-translation]: http://docs.videojs.com/tutorial-languages.html
[cast-receiver]: https://developers.google.com/cast/docs/receiver_apps
[def-cast-id]: https://developers.google.com/cast/docs/receiver_apps#default
[player-source]: http://docs.videojs.com/Player.html#currentSource
[custom-receiver]: https://developers.google.com/cast/docs/custom_receiver
[contributing]: https://github.com/silvermine/silvermine-info#contributing
[chromecast-load-media]: https://developers.google.com/cast/docs/reference/web_sender/cast.framework.CastSession#loadMedia
[chromecast-load-request]: https://developers.google.com/cast/docs/reference/web_sender/chrome.cast.media.LoadRequest