使用媒體音軌

媒體軌可以是音訊或影片串流物件,也可以是文字物件 (副標題) 或說明文字)。

GCKMediaTrack 則代表軌跡。由專屬數字 ID 和 例如 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)
敬上
目標-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()
敬上
目標-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];

啟用一或多個與媒體項目相關聯的曲目 (在 呼叫 -[setActiveTrackIDs:],即可在 GCKRemoteMediaClient 並傳送要啟用的曲目 ID。例如,下列 程式碼就會啟用上方建立的字幕軌。

Swift
sessionManager.currentSession?.remoteMediaClient?.setActiveTrackIDs([1])
敬上
目標-C
[self.sessionManager.currentSession.remoteMediaClient setActiveTrackIDs:@[@1]];

如要在目前的媒體項目上停用曲目,請呼叫 -[setActiveTrackIDs:]已開啟 GCKRemoteMediaClient 缺少陣列或 nil下列程式碼會停用字幕軌。

Swift
sessionManager.currentSession?.remoteMediaClient?.setActiveTrackIDs([])
敬上
目標-C
[self.sessionManager.currentSession.remoteMediaClient setActiveTrackIDs:@[]];

設定文字軌的樣式

GCKMediaTextTrackStyle敬上 類別會封裝文字軌的樣式資訊。字幕軌樣式可以是 透過呼叫 -[GCKRemoteMediaClient setTextTrackStyle]。 下列程式碼中建立的軌跡樣式,在不透明度 50% 的情況下,將文字變成紅色 (FF) (80) 並設定 Serif 字型。

Swift
let textTrackStyle = GCKMediaTextTrackStyle.createDefault()
textTrackStyle.foregroundColor = GCKColor.init(cssString: "#FF000080")
textTrackStyle.fontFamily = "serif"
styleChangeRequest = sessionManager.currentSession?.remoteMediaClient?.setTextTrackStyle(textTrackStyle)
styleChangeRequest?.delegate = self
敬上
目標-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
  }
}
敬上
目標-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 串流和媒體串流因此,如果沒有 CORS 標頭 針對伺服器上的簡易 mp4 媒體,然後新增簡單的子標題 您必須更新伺服器,才能串流播放媒體 加入適當的 CORS 標頭此外,您必須允許 並分別指定 Content-Type、Accept-Encoding 和 Range請注意, 最後兩個標頭是您先前可能不需要的額外標頭。