Skip to content

Instantly share code, notes, and snippets.

@josuesasilva
Last active September 20, 2023 23:49
Show Gist options
  • Save josuesasilva/e56ce6550f5cc00edf053b5a38e622a3 to your computer and use it in GitHub Desktop.
Save josuesasilva/e56ce6550f5cc00edf053b5a38e622a3 to your computer and use it in GitHub Desktop.
A sample implementation for AVAssetResourceLoaderDelegate
import AVKit
class VideoPlayer {
// ...
func start() {
requestURLAsset(for: src, cookies: cookies, delegate: self) { [weak self] asset in
let playerItem = AVPlayerItem(asset: asset)
DispatchQueue.main.async {
self.playVideo()
}
}
}
private func requestURLAsset(
for urlString: String?,
cookies: [[String: String]],
delegate: AVAssetResourceLoaderDelegate?,
then handler: @escaping (AVURLAsset?) -> Void
) {
// ...
let cookies = loadCookies()
let asset = AVURLAsset(url: videoUrl, options: cookies)
asset.resourceLoader.setDelegate(delegate, queue: .global(qos: .default))
asset.loadValuesAsynchronously(forKeys: ["playable"]) {
handler(asset)
}
}
}
extension VideoPlayer: AVAssetResourceLoaderDelegate {
public func resourceLoader(_ resourceLoader: AVAssetResourceLoader,
shouldWaitForLoadingOfRequestedResource loadingRequest: AVAssetResourceLoadingRequest) -> Bool {
guard let videoUrl = loadingRequest.request.replacingRedirectScheme else {
return false
}
loadingRequest.process(url: videoUrl, withCookies: self.cookies)
return true
}
}
@Peter1119
Copy link

I've been stuck for a long time due to a problem updating cookies, so if you know anything, please let me know.

@josuesasilva
Copy link
Author

what is the 'process' method? I cannot find it. plz

It’s an extension to add cookies to the request

@josuesasilva
Copy link
Author

I've been stuck for a long time due to a problem updating cookies, so if you know anything, please let me know.

I don’t have access to this code anymore but I remember passing the cookie like in the web authenticating each chunk of the video.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment