Using Media Tracks

A media track can be an audio or video stream object, or a text object (subtitle or caption).

A GCKMediaTrack object represents a track. It consists of a unique numeric identifier and other attributes such as a content ID and title. A GCKMediaTrack instance can be created as follows:

GCKMediaTrack *captionsTrack =
      [[GCKMediaTrack alloc] initWithIdentifier:1
                              contentIdentifier:@"https://some-url/caption_en.vtt"
                                    contentType:@"text/vtt"
                                           type:GCKMediaTrackTypeText
                                    textSubtype:GCKMediaTextTrackSubtypeCaptions
                                           name:@"English Captions"
                                   languageCode:@"en"
                                     customData:nil];

A media item can have multiple tracks; for example, it can have multiple subtitles (each for a different language) or multiple alternative audio streams (for different languages). GCKMediaInformation is the class that represents a media item. To associate a collection of GCKMediaTrack objects with a media item, your app should update its mediaTracks property. Your app needs to make his association before it loads the media to the receiver, as in the following code.

NSArray *tracks = @[captionsTrack];

GCKMediaInformation *mediaInformation =
    [[GCKMediaInformation alloc] initWithContentID:@"https://some-url/"
                                                   "DesigningForGoogleCast.mp4"
                                        streamType:GCKMediaStreamTypeNone
                                       contentType:@"video/mp4"
                                          metadata:metadata
                                    streamDuration:0
                                       mediaTracks:tracks
                                    textTrackStyle:nil
                                        customData:nil];

Activate one or more tracks that were associated with the media item (after the media is loaded) by calling GCKRemoteMediaClient setActiveTrackIDs and passing the IDs of the tracks to be activated. For example, the following code activates the captions track created above.

[_remoteMediaClient setActiveTrackIDs:@[@1]];

To deactivate a track on the current media item, call -[setActiveTrackIDs:] on GCKRemoteMediaClient with an empty array or nil. The following code disables the captions track.

[_remoteMediaClient setActiveTrackIDs: @[]];

Styling Text Tracks

The GCKMediaTextTrackStyle class encapsulates the style information of a text track. A track style can be applied to the currently playing media item by calling GCKRemoteMediaClientsetTextTrackStyle. The track style created in the code below turns text red (FF) at 50% opacity (80) and sets a serif font.

GCKMediaTextTrackStyle *textTrackStyle = [GCKMediaTextTrackStyle createDefault];
[textTrackStyle setForegroundColor:[[GCKColor alloc] initWithCSSString:@"#FF000080"]];
[textTrackStyle setFontFamily:@"serif"];
_styleChangeRequest = [_mediaControlChannel setTextTrackStyle:textTrackStyle];
_styleChangeRequest.delegate = self;

You can use the returned GCKRequest object for tracking this request.

#pragma mark - GCKRequestDelegate

- (void)requestDidComplete:(GCKRequest *)request {
  if (request == _styleChangeRequest) {
    NSLog(@"Style update completed.");
    _styleChangeRequest = nil;
  }
}

See Status updates below for more information. Apps should allow users to update the style for text tracks, either using the settings provided by the system or by the app itself. There is a default style provided (in iOS 7 and later), which can be retrieved via the static method +[GCKMediaTextTrackStyle createDefault]. The following text track style elements can be changed:

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

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

To this end, your app should register a GCKRemoteMediaClientListener. If the GCKMediaTextTrackStyle of the current media changes, then all of the connected senders will be notified through both the mediaControlChannelDidUpdateMetadata: and the mediaControlChannelDidUpdateStatus: callbacks. In this case, the receiver classes do not verify whether the new style is different from the previous one and notifies all the connected senders regardless. If, however, the list of active tracks is updated, only the mediaControlChannelDidUpdateStatus in connected senders will be notified.

Satisfy 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 header. In addition, you need to allow at least the following headers: Content-Type, Accept-Encoding, and Range. Note that the last two headers are additional headers that you may not have needed previously.