媒体轨道可以是音频或视频流对象,也可以是文本对象(字幕)。
GCKMediaTrack 对象表示轨道。它包含一个唯一的数字标识符和其他属性,例如内容 ID 和影视内容名称。可以按如下方式创建 GCKMediaTrack 实例:
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)
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 属性。您的应用需要在将媒体加载到接收器之前建立此关联,如以下代码所示:
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()
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,激活与媒体项关联的一个或多个轨道(在媒体加载后)。例如,以下代码会激活上面创建的字幕轨道。
sessionManager.currentSession?.remoteMediaClient?.setActiveTrackIDs([1])
[self.sessionManager.currentSession.remoteMediaClient setActiveTrackIDs:@[@1]];
如需停用当前媒体项中的轨道,请对 GCKRemoteMediaClient 调用 -[setActiveTrackIDs:],并使用空数组或 nil。以下代码会停用字幕轨道。
sessionManager.currentSession?.remoteMediaClient?.setActiveTrackIDs([])
[self.sessionManager.currentSession.remoteMediaClient setActiveTrackIDs:@[]];
设置文本轨道样式
GCKMediaTextTrackStyle 类封装了文本轨道样式信息。通过调用 -[GCKRemoteMediaClient
setTextTrackStyle],可以将轨道样式应用于当前正在播放的媒体项。以下代码中创建的轨道样式将文本设置为红色 (FF),不透明度为 50% (80),并设置了衬线字体。
let textTrackStyle = GCKMediaTextTrackStyle.createDefault() textTrackStyle.foregroundColor = GCKColor.init(cssString: "#FF000080") textTrackStyle.fontFamily = "serif" styleChangeRequest = sessionManager.currentSession?.remoteMediaClient?.setTextTrackStyle(textTrackStyle) styleChangeRequest?.delegate = self
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 对象来跟踪此请求。
// MARK: - GCKRequestDelegate func requestDidComplete(_ request: GCKRequest) { if request == styleChangeRequest { print("Style update completed.") styleChangeRequest = nil } }
#pragma mark - GCKRequestDelegate - (void)requestDidComplete:(GCKRequest *)request { if (request == self.styleChangeRequest) { NSLog(@"Style update completed."); self.styleChangeRequest = nil; } }
如需了解详情,请参阅下文的状态更新。应用应允许用户使用系统提供的设置或应用本身的设置来更新文本轨道样式。系统会提供默认样式,您可以通过静态方法 +[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。请注意,最后两个标头是您之前可能不需要的额外标头。