媒體音軌可以是音訊或視訊串流物件,或文字物件 (字幕)。
GCKMediaTrack
物件代表軌跡。其中包含專屬數字 ID 和其他內容,例如內容 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
上使用空陣列或 nil 呼叫 -[setActiveTrackIDs:]
。以下程式碼會停用字幕軌。
sessionManager.currentSession?.remoteMediaClient?.setActiveTrackIDs([])
[self.sessionManager.currentSession.remoteMediaClient setActiveTrackIDs:@[]];
設定文字樣式
GCKMediaTextTrackStyle
類別會封裝文字軌的樣式資訊。您可以呼叫 -[GCKRemoteMediaClient
setTextTrackStyle]
,將播放樣式套用至目前播放的媒體項目。下方程式碼中建立的追蹤樣式會將文字 (紅色) 變成 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; } }
詳情請參閱下方的狀態更新一節。應用程式應讓使用者使用系統提供的設定或應用程式本身,更新文字軌的樣式。提供預設樣式 (在 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-Unicode 和 Range。請注意,最後兩個標頭是您可能不再需要的額外標頭。