我正在创建一个在 iOS 中下载 M3u8 文件的函数。我可以下载 M3u8,但我希望能够在应用程序暂停或退出时恢复挂起的下载。以下是我使用的主要文档,但我无法让恢复功能按预期工作。
以下是我用来下载 M3U8 的内容:
private var config: URLSessionConfiguration!
var downloadSession: AVAssetDownloadURLSession!
var downloadTask: AVAssetDownloadTask!
var mediaSelectionMap = [AVAssetDownloadTask : AVMediaSelection]()
let downloadIdentifier = "mySession"
func assetDownload(url: URL) {
print("Download started...")
//Create new background session configuration
let configuration = URLSessionConfiguration.background(withIdentifier: downloadIdentifier)
//Create a new AVAssetDownloadURLSession with background config, delegate and queue
let downloadSession = AVAssetDownloadURLSession(configuration: configuration, assetDownloadDelegate: self, delegateQueue: OperationQueue.main)
downloadSession.getAllTasks { (taskArray) in
//Task array always nil when segment should be found
if taskArray.count > 0 {
print("Check if to resume previous download")
self.restorePendingDownloads()
} else {
let asset = AVURLAsset(url: url)
print("New Movie downloading")
//Create new AVAssetDownloadTask for the desired asset
self.downloadTask = downloadSession.makeAssetDownloadTask(asset: asset, assetTitle: "VideoSample", assetArtworkData: nil, options: [AVAssetDownloadTaskMinimumRequiredMediaBitrateKey: NSNumber(value: 0)])
//Start task and begin download
self.downloadTask?.resume()
}
}
}
以下是恢复挂起功能:
func restorePendingDownloads() {
print("restore pending download...")
// Create session configuration with ORIGINAL download identifier
config = URLSessionConfiguration.background(withIdentifier: downloadIdentifier)
// Create a new AVAssetDownloadURLSession
downloadSession = AVAssetDownloadURLSession(configuration: config,
assetDownloadDelegate: self,
delegateQueue: OperationQueue.main)
// Grab all the pending tasks associated with the downloadSession
downloadSession.getAllTasks { tasksArray in
print("getting all tasks.. \(tasksArray.count)") //returns 0
// For each task, restore the state in the app
for task in tasksArray {
guard let downloadTask = task as? AVAssetDownloadTask else { break }
// Restore asset, progress indicators, state, etc...
let asset = downloadTask.urlAsset
print("asset:\(asset)")
//NEVER CALLED BECAUSE EMPTY
}
}
}
有人建议使用下面的功能,但这只是将下载重置为开始。
func restorePendingDownload(url: URL){
let urlAsset = AVURLAsset(url: url)
downloadTask = downloadSession.makeAssetDownloadTask(asset: urlAsset, assetTitle: "master", assetArtworkData: nil, options: nil)
downloadTask.resume()
}
更新
我找到了暂停恢复功能的解决方案,但没有找到应用程序退出时如何恢复的解决方案。
我添加了这个函数,它在暂停时存储目标 URL,可以在恢复挂起函数中使用。
func urlSession(_ session: URLSession, assetDownloadTask: AVAssetDownloadTask, didFinishDownloadingTo location: URL) {
// Do not move the asset from the download location
UserDefaults.standard.set(location.relativePath, forKey: "assetPath")
destinationURL = location
}
在下载资产功能中,我更改了以下行:
self.downloadTask = downloadSession.makeAssetDownloadTask(asset: asset, assetTitle: "VideoSample", assetArtworkData: nil, options: [AVAssetDownloadTaskMinimumRequiredMediaBitrateKey: NSNumber(value: 0)])
至:
self.downloadTask = downloadSession.makeAssetDownloadTask(asset: asset, assetTitle: "VideoSample", assetArtworkData: nil, options: nil)
我认为设置最低要求是导致问题的原因。
退出应用程序时如何恢复挂起下载的任何帮助或指导都会令人惊叹。谢谢