首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swift -恢复挂起的M3U8下载不起作用

Swift -恢复挂起的M3U8下载不起作用
EN

Stack Overflow用户
提问于 2019-09-30 09:10:18
回答 1查看 573关注 0票数 2

我正在创建一个用iOS下载M3u8文件的函数。我可以下载M3u8,但我希望能够在应用程序暂停或退出时恢复挂起的下载。下面是我使用的主要文档,但我无法使恢复功能按预期工作。

https://developer.apple.com/library/archive/documentation/AudioVideo/Conceptual/MediaPlaybackGuide/Contents/Resources/en.lproj/HTTPLiveStreaming/HTTPLiveStreaming.html#//apple_ref/doc/uid/TP40016757-CH11-SW3

下面是我用来下载M3U8的代码:

代码语言:javascript
复制
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()
        }
    }
}

下面是恢复挂起功能:

代码语言:javascript
复制
 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
        }
    }
}

有人推荐使用下面的功能,但这只是将下载重置为开始。

代码语言:javascript
复制
func restorePendingDownload(url: URL){
    let urlAsset = AVURLAsset(url: url)
    downloadTask = downloadSession.makeAssetDownloadTask(asset: urlAsset, assetTitle: "master", assetArtworkData: nil, options: nil)
    downloadTask.resume()
}

更新

我找到了暂停恢复功能的解决方案,但没有找到在应用程序退出时如何恢复的解决方案。

我添加了这个函数,它在暂停时存储destinationURL,并且可以在restore pending功能中使用。

代码语言:javascript
复制
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 
}

在donwload资产函数中,我将以下行从:

代码语言:javascript
复制
  self.downloadTask = downloadSession.makeAssetDownloadTask(asset: asset, assetTitle: "VideoSample", assetArtworkData: nil, options: [AVAssetDownloadTaskMinimumRequiredMediaBitrateKey: NSNumber(value: 0)])

至:

代码语言:javascript
复制
 self.downloadTask = downloadSession.makeAssetDownloadTask(asset: asset, assetTitle: "VideoSample", assetArtworkData: nil, options: nil)

我认为设置最低要求是导致问题的原因。

任何在退出应用程序时如何恢复挂起的下载的帮助或方向都将是令人惊叹的。谢谢

EN

回答 1

Stack Overflow用户

发布于 2020-04-24 02:28:09

在HLSCatalog示例中,我尝试在创建AVAssetDownloadTask时将AVAssetDownloadTaskMinimumRequiredMediaBitrateKey: 265_000添加到选项中。

在AppDelegate中,我调用restorePending(),getAllTasks返回挂起的任务数。在下一步中,我仍然收到错误,我仍在解决这些错误,但任务不是空的。

我希望它能对你有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58160427

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档