Last active
September 20, 2023 23:49
-
-
Save josuesasilva/e56ce6550f5cc00edf053b5a38e622a3 to your computer and use it in GitHub Desktop.
A sample implementation for AVAssetResourceLoaderDelegate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.