使用媒體音軌

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

GCKMediaTrack 物件代表曲目。當中包含不重複的數字 ID 和其他屬性,例如內容 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]];

如要針對目前媒體項目停用播放,請使用空白陣列或 nil 對 GCKRemoteMediaClient 呼叫 -[setActiveTrackIDs:]。下列程式碼會停用字幕軌。

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

設定文字軌樣式

GCKMediaTextTrackStyle 類別會封裝文字軌的樣式資訊。呼叫 -[GCKRemoteMediaClient setTextTrackStyle] 後,曲目樣式即可套用至目前播放的媒體項目。透過以下程式碼建立的歷程樣式,會將文字紅色 (FF) 變成不透明度 50% (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
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;
  }
}

詳情請參閱下方的「狀態更新」一節。應用程式應允許使用者透過系統或應用程式本身的設定更新文字軌的樣式。您可以透過靜態方法 +[GCKMediaTextTrackStyle createDefault] 擷取預設樣式 (在 iOS 7 以上版本中)。您可以變更下列文字追蹤樣式元素:

  • 前景 (文字) 顏色和不透明度
  • 背景色彩和透明度
  • 邊緣類型
  • 邊緣顏色
  • 字型比例
  • 字型系列
  • 字型樣式

接收狀態更新資訊

當多位寄件者連線到同一個接收器時,每個寄件者都必須瞭解接收端的變更,即使這些變更是由其他寄件者發起也一樣。

為確保傳送者能收到接收端的狀態更新,應用程式應註冊 GCKRemoteMediaClientListener。如果目前媒體的 GCKMediaTextTrackStyle 有所變更,則所有已連結的傳送者都會透過 -[remoteMediaClient:didUpdateMediaMetadata:]-[remoteMediaClient:didUpdateMediaStatus:] 回呼收到通知。在這種情況下,接收器 SDK 不會驗證新樣式是否與前一個樣式不同,並會通知所有已連結的寄件者。不過,如果更新使用中的測試群組清單,系統只會通知已連線的寄件者中的 -[remoteMediaClient:didUpdateMediaStatus:]

符合 CORS 需求條件

如要進行自動調整式媒體串流,Google Cast 需要使用 CORS 標頭。不過,即使是簡單的 mp4 媒體串流,只要含有曲目,就需要 CORS。如果想為任何媒體啟用曲目,您必須同時為音軌串流和媒體串流啟用 CORS。因此,如果您在伺服器上沒有適用於簡易 mp4 媒體的 CORS 標頭,然後新增簡單的字幕軌,您將無法串流媒體,除非您更新伺服器來加入適當的 CORS 標頭。此外,您至少須允許下列標頭:Content-Type、Accept-Encoding 和 Range。請注意,最後兩個標頭是之前您可能不需要的額外標頭。