使用媒體音軌

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

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]];

如要停用目前媒體項目的音軌,請在 GCKRemoteMediaClient 上呼叫 -[setActiveTrackIDs:],並傳遞空陣列或 nil。下列程式碼會停用字幕軌。

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

設定文字軌樣式

GCKMediaTextTrackStyle 類別會封裝文字軌的樣式資訊。呼叫 -[GCKRemoteMediaClient setTextTrackStyle] 即可將軌道樣式套用至目前播放的媒體項目。下方程式碼建立的軌道樣式會將文字設為不透明度 50% (80) 的紅色 (FF),並設定襯線字型。

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] 擷取。你可以變更下列文字軌樣式元素:

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

接收狀態更新

如果多個傳送端連線至同一個接收端,即使變更是由其他傳送端發起,每個傳送端也必須瞭解接收端的變更。

為確保傳送者能收到接收者的狀態更新,應用程式應註冊 GCKRemoteMediaClientListener。如果目前媒體的 GCKMediaTextTrackStyle 發生變化,所有連線的傳送者都會透過 -[remoteMediaClient:didUpdateMediaMetadata:] 和 -[remoteMediaClient:didUpdateMediaStatus:] 回呼收到通知。在這種情況下,接收器 SDK 不會驗證新樣式是否與先前的樣式不同,而是會通知所有已連線的傳送器。不過,如果更新的是有效曲目清單,只有已連結的寄件者會收到 -[remoteMediaClient:didUpdateMediaStatus:] 通知。

x0A>

符合 CORS 規定

對於自適應媒體串流,Google Cast 需要 CORS 標頭,但即使是簡單的 mp4 媒體串流,如果包含軌道,也需要 CORS。如要為任何媒體啟用軌道,請務必為軌道串流和媒體串流啟用 CORS。因此,如果伺服器上沒有簡單 mp4 媒體的 CORS 標頭,而您新增簡單的字幕軌,就必須更新伺服器以加入適當的 CORS 標頭,否則無法串流播放媒體。此外,您至少需要允許下列標頭:Content-Type、Accept-Encoding 和 Range。請注意,最後兩個標頭是額外標頭,您先前可能不需要。