簡言之,我是使用 Cocoapods 管理 Chromecast SDK 的:
$ vim Podfile
pod 'google-cast-sdk'
pod 'google-cast-remote-display-sdk'
接著寫一點程式,參考 CastVideos-ios 內的 CastDeviceController 範例了解整個流程。
總之,若要簡單播放一個 URL ,需要依序完成以下動作:
- 搜尋 chromecast device 並實作 GCKDeviceScannerListener 偵測裝置
- GCKFilterCriteria *filterCriteria = [GCKFilterCriteria criteriaForAvailableApplicationWithID:kGCKMediaDefaultReceiverApplicationID];
- self.chromecastScanner = [[GCKDeviceScanner alloc] initWithFilterCriteria:filterCriteria];
- [self.chromecastScanner addListener:self];
- [self.chromecastScanner startScan];
- 透過 - (void)deviceDidComeOnline:(GCKDevice *)device 偵測到後,進行連線
- self.chromecastManager = [[GCKDeviceManager alloc] initWithDevice:self.chooseDevice clientPackageName:[NSBundle mainBundle].bundleIdentifier];
- self.chromecastManager.delegate = self;
- [self.chromecastManager connect];
- 連線裝置後,透過 Launch application 跟 Chromecast Device 進行溝通
- [self.chromecastManager launchApplication:kGCKMediaDefaultReceiverApplicationID];
- 透過 - (void)deviceManager:(GCKDeviceManager *)deviceManager didConnectToCastApplication:(GCKApplicationMetadata *)applicationMetadata sessionID:(NSString *)sessionID launchedApplication:(BOOL)launchedApplication; 偵測完成 chromecast app 程式啟動並建立 GCKMediaControlChannel
- self.mediaControlChannel = [[GCKMediaControlChannel alloc] init];
- self.mediaControlChannel.delegate = self;
- [deviceManager addChannel:self.mediaControlChannel];
- [self.mediaControlChannel requestStatus];
- 播放 Resource,以 http://www.w3schools.com/html/mov_bbb.mp4 為例
- NSMutableArray *tracks = [[NSMutableArray alloc] init];
- GCKMediaMetadata *metadata = [[GCKMediaMetadata alloc] initWithMetadataType:GCKMediaMetadataTypeGeneric];
- GCKMediaInformation *mi = [[GCKMediaInformation alloc] initWithContentID:@"http://www.w3schools.com/html/mov_bbb.mp4" streamType:GCKMediaStreamTypeNone contentType:nil metadata:metadata streamDuration:0 mediaTracks:tracks textTrackStyle:[GCKMediaTextTrackStyle createDefault] customData:nil];
- [self.mediaControlChannel loadMedia:mi autoplay:YES];
如此一來 Chromecast device 就會播放指定的連結啦。
作者已經移除這則留言。
回覆刪除