我正在尝试使用AVContentKeySession播放加密的HLS流,但是没有实现播放,并且在日志中看到"NSURLConnection finished with error - code -1002“。AVFoundation未调用AVContentKeySessionDelegate上的委托方法。
如果在媒体资产上设置了资源加载器,则可以成功播放相同的流。
我尝试按如下方式播放
NSString *mediaUrl = [[NSUserDefaults standardUserDefaults] objectForKey:@"media_url"];
AVURLAsset *avUrlAsset = (AVURLAsset*)[AVAsset assetWithURL:[NSURL URLWithString: mediaUrl]];
AVContentKeySession *contentKeySession = [AVContentKeySession contentKeySessionWithKeySystem:AVContentKeySystemFairPlayStreaming];
[_contentKeySession setDelegate:[[PlaybackContentKeySessionDelegate alloc] init] queue:dispatch_get_main_queue()];
[_contentKeySession processContentKeyRequestWithIdentifier:@"skd://my_asset_id" initializationData:nil options:nil];
[playbackContentKeySession.contentKeySession addContentKeyRecipient:avUrlAsset];
avUrlAsset.resourceLoader.preloadsEligibleContentKeys = true;
AVPlayerItem *avPlayerItem = [AVPlayerItem playerItemWithAsset:avUrlAsset];
AVPlayer *avPlayer = [[AVPlayer alloc] initWithPlayerItem:avPlayerItem];
self.avPlayerViewController.player = avPlayer;
__weak ViewController *vc = self;
[self presentViewController:self.avPlayerViewController animated:YES completion:^(){
vc.avPlayerViewController.player play];
}];其中委托类如下所示。它是不完整的,但在这个阶段,我只希望看到委托方法被调用。
@interface PlaybackContentKeySessionDelegate : NSObject<AVContentKeySessionDelegate>
@end- (void)contentKeySession:(nonnull AVContentKeySession *)session didProvideContentKeyRequest:(nonnull AVContentKeyRequest *)keyRequest {
NSLog(@"contentKeySession");
}
@end但是,我看到了上面提到的错误消息。请注意,如果我设置了一个可以获取密钥的有效resourceLoader,则播放将顺利进行。
发布于 2019-06-07 00:49:21
对于遇到类似情况的其他任何人:问题是ContentKeySession将委托作为弱引用持有,这是委托模式的常规做法,因此它被清理掉了。当有人发现它的时候,我踢了自己!
https://stackoverflow.com/questions/56348114
复制相似问题