【问题标题】:The delegate method on AVContentKeySessionDelegate are not being called没有调用 AVContentKeySessionDelegate 上的委托方法
【发布时间】:2019-10-14 07:56:29
【问题描述】:

我正在尝试使用 AVContentKeySession 播放加密的 HLS 流,但没有实现播放,并且在日志中看到“NSURLConnection 完成并出现错误 - 代码 -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

但是,我看到了上面提到的错误消息。请注意,如果我设置了一个可以获取密钥的有效资源加载器,则播放不会出现任何问题。

【问题讨论】:

    标签: avfoundation http-live-streaming avcontentkeysession


    【解决方案1】:

    对于遇到类似情况的其他任何人:问题是 ContentKeySession 将委托作为弱引用,这是委托模式的传统,因此它正在被清理。 当有人发现它时,我踢了自己!

    【讨论】:

    • 我没有添加单独的委托类,而是在我的 ViewController 中实现了 AVContentKeySessionDelegate 的委托方法。我的代码 sn-p 是: let assets = AVURLAsset(url: videoUrl) self.contentKeySession = AVContentKeySession(keySystem: .fairPlayStreaming) self.contentKeySession?.setDelegate(self, queue: DispatchQueue.main) self.contentKeySession?.addContentKeyRecipient(asset ) 但是,委托方法没有被调用。
    猜你喜欢
    • 2020-11-14
    • 2012-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-03
    • 2020-11-28
    • 2017-05-04
    • 2012-11-10
    相关资源
    最近更新 更多