Search code examples
iosswiftavplayerfairplay

Store ckc key data of AVURLAsset without play video


I want to call programmatically the

func resourceLoader(_ resourceLoader: AVAssetResourceLoader, shouldWaitForLoadingOfRequestedResource loadingRequest: AVAssetResourceLoadingRequest) -> Bool

of an AVURLAsset from the delegate of the AVPlayer, AVAssetResourceLoaderDelegate, so that I can cash the ckc Data so that I can play decrypted content offline.

I followed the steps of https://developer.apple.com/videos/play/wwdc2016/504/

If I play the AVURLAsset cash the ckc key data and then close the internet and play the AVURLAsset again it works and I can see the video offline.

But I need to call the resourseLoader function without to play the video.

The reason I can't do the steps is because to get the ckc data I need the loadingRequest object from the delegate function resourceLoader.

Is there any way to accomplish that without call the play method and discard player when I store the keys?


Solution

  • Maybe to late for you, I had the same problem. Try set add preloadEligibleContentKeys to true to your resource loader when you download.

    let asset = AVURLAsset(url: url)        
    // ADD THIS  LINE TO PRELOAD BEFORE PLAYING
    asset.resourceLoader.preloadsEligibleContentKeys = true
    asset.resourceLoader.setDelegate(self, queue: DispatchQueue.main)
    
    let downloadTask = downloadSession?.makeAssetDownloadTask(asset: asset, assetTitle: "Asset title", assetArtworkData: nil, options: nil)