Silvermine VideoJS Chromecast Plugin

repository·master·Indexed 41 days ago

https://github.com/silvermine/videojs-chromecast

A 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.

Tokens
3.8K
Snippets
11
Records
20
Agent score
100%

What's inside videojs-chromecast

  1. Configure the Chromecast plugin for Video.js

    master

    To use the plugin, you must configure the Video.js player.

    Critical Requirement: You must set the techOrder option 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();
  2. Initialize preloadWebComponents for jQuery compatibility

    master

    If your project uses jQuery, you should set preloadWebComponents: true.

    This prevents race conditions where the Chromecast framework loads the webcomponents.js polyfill dynamically, which can break jQuery's event bubbling. When enabled, the plugin loads webcomponents-lite.js synchronously.

    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>
  3. Install and integrate @silvermine/videojs-chromecast

    master

    You can integrate the plugin using two primary methods:

    Install the package via npm:

    npm install @silvermine/videojs-chromecast

    Then, 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);
  4. Customize Chromecast styles with SCSS

    master

    If 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.

  5. Configure chromecast tech options

    master

    These functions allow you to pass dynamic data to the Chromecast receiver during a session.

    OptionTypeDescription
    chromecast.requestTitleFnfunctionReceives source object; returns a string for the UI title.
    chromecast.requestSubtitleFnfunctionReceives source object; returns a string for the UI subtitle.
    chromecast.requestCustomDataFnfunctionReceives source object; returns an object for custom receiver data.
    chromecast.modifyLoadRequestFnfunctionReceives LoadRequest object; returns modified LoadRequest. 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: {}
       }
    };
  6. Configure plugin.chromecast options

    master

    These options control the behavior of the Chromecast plugin UI and connection.

    OptionTypeDefaultDescription
    plugins.chromecast.receiverAppIDstringdefault IDThe ID of a custom Chromecast receiver app.
    plugins.chromecast.addButtonToControlBarbooleantrueWhether to automatically add the Chromecast button to the control bar.
    plugins.chromecast.buttonPositionIndexnumberlast childZero-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.
  7. Register the Chromecast Tech with Video.js

    master

    The chromecast Tech is a Video.js Tech implementation that enables playback on Google Chromecast devices. To use it, you must pass the videojs instance to the module export, which registers the chromecast tech globally. Once registered, Video.js will automatically select this tech if ChromecastTech.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');
  8. Use the `addCastLabelToButton` option for the Chromecast button

    master

    When initializing the ChromecastButton, you can pass the addCastLabelToButton option.

    • If true: The button will include a text label (e.g., "Cast" or "Disconnect Cast") and use the CSS class vjs-chromecast-button-lg.
    • If false (default): The button behaves as a standard icon button with the control text "Open Chromecast menu".
  9. Configure ChromecastTech via initialization options

    master

    When initializing the chromecast Tech, you can provide several callback functions in the options object 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 the source object and returns a string for the media title.
    • requestSubtitleFn: A function that receives the source object and returns a string for the media subtitle.
    • requestCustomDataFn: A function that receives the source object and returns an object for chrome.cast.media.LoadRequest.customData.
    • modifyLoadRequestFn: A function that receives the chrome.cast.media.LoadRequest object and must return a modified (or original) LoadRequest object.
    • 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' }
    };
  10. Configure the videojs-chromecast plugin options

    master

    When initializing the plugin, you can pass a userOpts object to configure its behavior.

    Currently, the following option is supported:

    OptionTypeDefaultDescription
    preloadWebComponentsbooleanfalseIf set to true, the plugin will explicitly call preloadWebComponents() to prepare necessary web components.
    // Example configuration
    videojsChromecast(videojs, {
      preloadWebComponents: true
    });
  11. Configure the position of the Chromecast button in the control bar

    master

    The chromecastButton is 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 the controlBar.children array when initializing the player.

    videojs('playerID', {
       controlBar: {
          children: [
             'playToggle',
             'progressControl',
             'volumePanel',
             'fullscreenToggle',
             'chromecastButton',
          ],
       },
    });