Hi everyone!
I'm developing an application that will show a stream video feed (Instagram like feed) and I was thinking about how to make it.
Actually I am using a CollectionViewController and instantiating AVPlayers inside each cell.
I'm also managing pause/resume task on scroll view scrolling.
Videos come from cdn and are m3u8 format (for HLS).
When I play with
let player = AVPlayer(url: videoURL)
Everything works good.
But what I really need is to cache this video in order to improve performance and reactivity.
I tried with AVAssetDownloadURLSession, following this guide https://developer.apple.com/library/content/documentation/AudioVideo/Conceptual/MediaPlaybackGuide/Contents/Resources/en.lproj/HTTPLiveStreaming/HTTPLiveStreaming.html
Strange behavior: the first time I launched the app, it worked! Now, it does not.
I also found out that downloading is not monitored (print (percentageComplete) does not work!)
This is the code I used:
func setupAssetDownload(url:URL) {
/
let configuration = URLSessionConfiguration.background(withIdentifier: "streamVideo")
/
let downloadSession = AVAssetDownloadURLSession(configuration: configuration,
assetDownloadDelegate: self,
delegateQueue: OperationQueue.main)
let asset = AVURLAsset(url: url)
/
let downloadTask = downloadSession.makeAssetDownloadTask(asset: asset,
assetTitle: "",
assetArtworkData: nil,
options: nil)
/
downloadTask?.resume()
}
func urlSession(_ session: URLSession, assetDownloadTask: AVAssetDownloadTask, didLoad timeRange: CMTimeRange, totalTimeRangesLoaded loadedTimeRanges: [NSValue], timeRangeExpectedToLoad: CMTimeRange) {
var percentComplete = 0.0
/
for value in loadedTimeRanges {
/
let loadedTimeRange = value.timeRangeValue
/
percentComplete += loadedTimeRange.duration.seconds / timeRangeExpectedToLoad.duration.seconds
}
percentComplete *= 100
print("Percent complete \(percentComplete)")
/
}
func urlSession(_ session: URLSession, assetDownloadTask: AVAssetDownloadTask, didFinishDownloadingTo location: URL) {
/
UserDefaults.standard.set(location.relativePath, forKey: "assetPath")
}
What's wrong with this code?
Thanks for your help, it's very urgent!