Add Advanced Features to Your Chrome App

Using the tracks APIs

A track can be a text (subtitle or caption) object, or an audio or video stream object. The Tracks APIs let you work with these objects in your application.

A Track object represents a track in the SDK. You can configure a track and assign a unique ID to it like this:

var englishSubtitle = new, // track ID;
englishSubtitle.trackContentId = 'https://some-url/caption_en.vtt';
englishSubtitle.trackContentType = 'text/vtt';
englishSubtitle.subtype =; = 'English Subtitles';
englishSubtitle.language = 'en-US';
englishSubtitle.customData = null;

var frenchSubtitle = new, // track ID;
frenchSubtitle.trackContentId = 'https://some-url/caption_fr.vtt';
frenchSubtitle.trackContentType = 'text/vtt';
frenchSubtitle.subtype =; = 'French Subtitles';
frenchSubtitle.language = 'fr';
frenchSubtitle.customData = null;

var frenchAudio = new, // track ID;
frenchAudio.trackContentId = 'trk0001';
frenchAudio.trackContentType = 'audio/mp3';
frenchAudio.subtype = null; = 'French Audio';
frenchAudio.language = 'fr';
frenchAudio.customData = null;

A media item may have multiple tracks; for example, it can have multiple subtitles (each for a different language) or multiple alternative audio streams (for different languages).

MediaInfo is the class that models a media item. To associate a collection of Track objects with a media item, you update its tracks property. This association needs to be made before the media is loaded to the receiver:

var tracks = [englishSubtitle, frenchSubtitle, frenchAudio];
var mediaInfo = new;
mediaInfo.contentType = 'video/mp4';
mediaInfo.metadata = new;
mediaInfo.customData = null;
mediaInfo.streamType =;
mediaInfo.textTrackStyle = new;
mediaInfo.duration = null;
mediaInfo.tracks = tracks;

You can set the active tracks in the media activeTrackIds request.

You can also activate one or more tracks that were associated with the media item, after the media is loaded, by calling EditTracksInfoRequest(opt_activeTrackIds, opt_textTrackStyle) and passing the IDs of the tracks to be activated in opt_activeTrackIds. Note, both parameters are optional and you may choose which, active tracks or styles, to set at your discretion. For example, here is how to activate the French subtitle (2) and French audio (3):

var activeTrackIds = [2, 3];
var tracksInfoRequest = new;
media.editTracksInfo(tracksInfoRequest, successCallback, errorCallback);

To remove all audio or video tracks from the current media, simply set mediaInfo.tracks=null (an empty array) and reload the media.

To remove all text tracks from the current media (for example, turning off subtitles), do one of the following:

  • Update var activeTrackIds = [2, 3]; (shown previously) so it includes [3] only, the audio track.
  • Set mediaInfo.tracks=null. Note that it's not necessary to reload the media to turn off text captions (track.hidden). Sending an activeTracksId array that doesn't contain a previously enabled trackId disables the text track.

Styling text tracks

TextTrackStyle is the object that encapsulates the styling information of a text track. After creating or updating an existing TextTrackStyle object, you can apply that to the currently playing media item by calling its editTrackInfo

method, like this:

var textTrackStyle = new;
var tracksInfoRequest = new;
media.editTracksInfo(tracksInfoRequest, successCallback, errorCallback);

You can track the status of the request with result of the callbacks, either success or error, and update the originating sender accordingly.

Applications should allow users to update the style for text tracks, either using the settings provided by the system or by the application itself.

You can style the following text track style elements:

  • Foreground (text) color and opacity
  • Background color and opacity
  • Edge type
  • Edge Color
  • Font Scale
  • Font Family
  • Font Style

For example, set the text color to red with 75% opacity as follows:

var textTrackStyle = new;
textTrackStyle.foregroundColor = '#80FF0000';

Volume control

You can use the RemotePlayer and RemotePlayerController to set the receiver volume.

function changeVolume(newVolume) {
  player.volumeLevel = newVolume;
  // Update sender UI to reflect change

The sender app should adhere to the following guidelines for controlling volume:

  • The sender application must synchronize with the receiver so that the sender UI always reports the volume per the receiver. Use the RemotePlayerEventType.VOLUME_LEVEL_CHANGED and RemotePlayerEventType.IS_MUTED_CHANGED callback to maintain the volume on the sender. See Status updates for more information.
  • Sender apps must not set the volume level at a specific, pre-defined level or set the volume level to the sender device's ringer/media volume when the app loads on the receiver.

See Sender volume controls in the Design Checklist.

Sending media messages to the receiver

Media Messages can be sent from the sender to receiver. For example, to send a SKIP_AD message to the receiver:

const skipButton = document.getElementById('skip');  //to get a handle to skip button element
skipButton.addEventListener("click", function(){
    const media = castSession.getMediaSession();
    castSession.sendMessage('', {
      type: 'SKIP_AD',
      requestId: 1,
      mediaSessionId: media.mediaSessionId

Status updates

When multiple senders are connected to the same receiver, it is important for each sender to be aware of the changes on the receiver even if those changes were initiated from other senders.

Note: This is important for all apps, not only those that explicitly support multiple senders, because some Cast devices have control inputs (remotes, buttons) that behave as virtual senders, affecting the status on the receiver.

To this end, your application should register all necessary listeners on the RemotePlayerController. If the TextTrackStyle of the current media changes, then all of the connected senders will be notified and the corresponding properties of the current media session, such as activeTrackIds and textTrackStyle of the MediaInfo field will be sent to senders in callbacks. In this case, the receiver SDK does not verify whether the new style is different from the previous one and notifies all the connected senders regardless.

Progress indicator

Showing the playback location with a progress indicator on the sender is a requirement for most apps. The Cast APIs use the Cast media protocol, which optimizes bandwidth consumption for this and other scenarios, so you do not need to implement your own status synchronization. For the proper implementation of a progress indicator for media playback using the APIs, see the CastVideos-chrome sample app.

CORS requirements

For adaptive media streaming, Google Cast requires the presence of CORS headers, but even simple mp4 media streams require CORS if they include Tracks. If you want to enable Tracks for any media, you must enable CORS for both your track streams and your media streams. So, if you do not have CORS headers available for your simple mp4 media on your server, and you then add a simple subtitle track, you will not be able to stream your media unless you update your server to include the appropriate CORS headers.

You need the following headers: Content-Type,Accept-Encoding, and Range. Note that the last two headers, Accept-Encoding and Range, are additional headers that you may not have needed previously.

Wildcards "*" cannot be used for the Access-Control-Allow-Origin header. If the page has protected media content, it must use a domain instead of a wildcard.

Next steps

This concludes the features that you can add to your Chrome sender app. You can now build a sender app for another platform (Android or iOS), or build a receiver app.