我创建一个后台会话,如下所示:
let backgroundConfiguration = URLSessionConfiguration.background(withIdentifier: backgroundSessionId)
backgroundConfiguration.isDiscretionary = false
backgroundConfiguration.sessionSendsLaunchEvents = true backgroundConfiguration.shouldUseExtendedBackgroundIdleMode = true
privateQueue = OperationQueue()
privateQueue.maxConcurrentOperationCount = 1
assetDownloadSession = AVAssetDownloadURLSession(configuration: backgroundConfiguration, assetDownloadDelegate: self, delegateQueue: privateQueue)还要创建并运行任务:
let task = assetDownloadSession.makeAssetDownloadTask(asset: urlAsset, assetTitle: title, assetArtworkData: nil, options: nil)
task.resume()但是,如果我的实际设备上的磁盘空间少于500MB,应用程序将重新启动,并调用以下方法:
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?)但是下一个方法没有被调用:
func urlSession(_ session: URLSession, assetDownloadTask: AVAssetDownloadTask, didFinishDownloadingTo location: URL)该文件未从设备中删除,如何知道它在本地的位置以及如何将其删除?系统不会自动执行此操作。
在真实的iPhone6s,iOS 14.0.1日志和Mac控制台应用程序上检查了许多次。
如果内存大于500MB,则一切正常,并调用didCompleteWithError方法
发布于 2020-11-04 04:03:17
您可以在URLSession:aggregateAssetDownloadTask:willDownloadToURL:中使用AVAggregateAssetDownloadTask获取下载媒体的位置,例如:
var assetDownloadURLSession: AVAssetDownloadURLSession!
var task = AVAggregateAssetDownloadTask?
var downloadURL: URL?
func download(asset: AVURLAsset)
let backgroundConfiguration = URLSessionConfiguration.background(withIdentifier: "AAPL-Identifier")
assetDownloadURLSession = AVAssetDownloadURLSession(configuration: backgroundConfiguration, assetDownloadDelegate: self, delegateQueue: OperationQueue.main)
task = assetDownloadURLSession.aggregateAssetDownloadTask(with: asset, ...)
task?.resume()
...
}
func urlSession(_ session: URLSession, aggregateAssetDownloadTask: AVAggregateAssetDownloadTask, willDownloadTo location: URL) {
downloadURL = location
}
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
if let url = downloadURL {
do {
try FileManager.default.removeItem(at: url)
}
catch {
print(error)
}
}
}如何处理聚合任务您可以查看苹果的示例项目:https://developer.apple.com/documentation/avfoundation/media_playback_and_selection/using_avfoundation_to_play_and_persist_http_live_streams
https://stackoverflow.com/questions/64506831
复制相似问题