使用媒体轨道

媒体轨道可以是音频或视频流对象,也可以是文本对象(字幕)。

GCKMediaTrack 对象代表轨道。它由唯一数字标识符和其他属性(如 Content ID 和标题)组成。您可以按如下方式创建 GCKMediaTrack 实例:

Swift
let captionsTrack = GCKMediaTrack.init(identifier: 1,
                                       contentIdentifier: "https://some-url/caption_en.vtt",
                                       contentType: "text/vtt",
                                       type: GCKMediaTrackType.text,
                                       textSubtype: GCKMediaTextTrackSubtype.captions,
                                       name: "English Captions",
                                       languageCode: "en",
                                       customData: nil)
Objective-C
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];

一个媒体项可以有多个轨道;例如,它可以有多个字幕(分别对应不同的语言)或多个备用音频流(适用于不同的语言)。 GCKMediaInformation 是表示媒体项的类。如需将一系列 GCKMediaTrack 对象与媒体内容相关联,应用应更新其 mediaTracks 属性。应用需要先建立此关联,然后才能将媒体加载到接收器,如以下代码所示:

Swift
let tracks = [captionsTrack]

let url = URL.init(string: "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4")
guard let mediaURL = url else {
  print("invalid mediaURL")
  return
}

let mediaInfoBuilder = GCKMediaInformationBuilder.init(contentURL: mediaURL)
mediaInfoBuilder.streamType = GCKMediaStreamType.none;
mediaInfoBuilder.contentType = "video/mp4"
mediaInfoBuilder.metadata = metadata;
mediaInfoBuilder.mediaTracks = tracks;
mediaInformation = mediaInfoBuilder.build()
Objective-C
NSArray *tracks = @[captionsTrack];

GCKMediaInformationBuilder *mediaInfoBuilder =
  [[GCKMediaInformationBuilder alloc] initWithContentURL:
   [NSURL URLWithString:@"https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"]];
mediaInfoBuilder.streamType = GCKMediaStreamTypeNone;
mediaInfoBuilder.contentType = @"video/mp4";
mediaInfoBuilder.metadata = metadata;
mediaInfoBuilder.mediaTracks = tracks;
self.mediaInformation = [mediaInfoBuilder build];

通过在 GCKRemoteMediaClient 上调用 -[setActiveTrackIDs:] 并传递要激活的轨道的 ID,启用与媒体内容关联的一个或多个轨道。例如,以下代码可激活上面创建的字幕轨道。

Swift
sessionManager.currentSession?.remoteMediaClient?.setActiveTrackIDs([1])
Objective-C
[self.sessionManager.currentSession.remoteMediaClient setActiveTrackIDs:@[@1]];

如需停用当前媒体项上的曲目,请在 GCKRemoteMediaClient 上调用 -[setActiveTrackIDs:] 并传入空数组或 nil。以下代码可停用字幕轨道。

Swift
sessionManager.currentSession?.remoteMediaClient?.setActiveTrackIDs([])
Objective-C
[self.sessionManager.currentSession.remoteMediaClient setActiveTrackIDs:@[]];

设置文字轨道的样式

GCKMediaTextTrackStyle 类可封装文本轨道的样式信息。可通过调用 -[GCKRemoteMediaClient setTextTrackStyle] 将轨道样式应用于当前正在播放的媒体项。在以下代码中创建的轨道样式会以 50% 的不透明度 (80) 将文本红色 (FF) 变为 Serif 字体。

Swift
let textTrackStyle = GCKMediaTextTrackStyle.createDefault()
textTrackStyle.foregroundColor = GCKColor.init(cssString: "#FF000080")
textTrackStyle.fontFamily = "serif"
styleChangeRequest = sessionManager.currentSession?.remoteMediaClient?.setTextTrackStyle(textTrackStyle)
styleChangeRequest?.delegate = self
Objective-C
GCKMediaTextTrackStyle *textTrackStyle = [GCKMediaTextTrackStyle createDefault];
[textTrackStyle setForegroundColor:[[GCKColor alloc] initWithCSSString:@"#FF000080"]];
[textTrackStyle setFontFamily:@"serif"];
self.styleChangeRequest = [self.sessionManager.currentSession.remoteMediaClient setTextTrackStyle:textTrackStyle];
self.styleChangeRequest.delegate = self;

您可以使用返回的 GCKRequest 对象来跟踪此请求。

Swift
// MARK: - GCKRequestDelegate

func requestDidComplete(_ request: GCKRequest) {
  if request == styleChangeRequest {
    print("Style update completed.")
    styleChangeRequest = nil
  }
}
Objective-C
#pragma mark - GCKRequestDelegate

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

如需了解详情,请参阅下文的状态更新。应用应允许用户使用系统或应用本身提供的设置更新文字轨道的样式。系统提供了默认样式(在 iOS 7 及更高版本中),可通过静态方法 +[GCKMediaTextTrackStyle createDefault] 检索该样式。可以更改以下文本轨道样式元素:

  • 前景(文本)颜色和不透明度
  • 背景颜色和不透明度
  • 边缘类型
  • 边缘颜色
  • 字体缩放
  • 字体系列
  • 字体样式

接收状态更新

当多个发送者连接到同一个接收器时,每个发送者都务必要了解接收器上的更改,即使这些更改是由其他发送者发起的。

为了确保您的发送器能够收到来自接收器的状态更新,您的应用应注册 GCKRemoteMediaClientListener。如果当前媒体的 GCKMediaTextTrackStyle 发生更改,所有连接的发送器都将通过 -[remoteMediaClient:didUpdateMediaMetadata:]-[remoteMediaClient:didUpdateMediaStatus:] 回调收到通知。在这种情况下,接收器 SDK 不会验证新样式是否与前一种样式不同,也不会通知所有已连接的发送方。不过,如果活跃轨道列表已更新,则只会通知已连接的发送器中的 -[remoteMediaClient:didUpdateMediaStatus:]

满足 CORS 要求

对于自适应媒体流式传输,Google Cast 要求具有 CORS 标头;但即使是简单的 mp4 媒体流,如果其包含轨道,也需要 CORS。如果要为任何媒体启用跟踪,您必须同时为跟踪流和媒体流启用 CORS。因此,如果您的服务器上没有适用于简单 mp4 媒体的 CORS 标头,然后您添加了简单的字幕轨道,那么您将无法流式传输您的媒体,除非您更新服务器以包含相应的 CORS 标头。此外,您至少需要允许以下标头:Content-Type、Accept-Encoding 和 Range。请注意,最后两个头文件是您之前可能不需要的额外头文件。