Swift - 恢复待下载的M3U8不能正常工作

问题描述 投票:0回答:1

我正在iOS中创建一个下载M3u8文件的功能。我可以下载M3u8,但我希望能够在应用暂停或退出时恢复待下载的功能。以下是我使用的主要文档,但我无法让还原功能按照预期工作。

https:/developer.apple.comlibraryarchivedocumentationAudioVideoConceptualMediaPlaybackGuideContentsResourcesen.lprojHTTPLiveStreamingHTTPLiveStreaming.html#/apple_refdocuidTP40016757-CH11-SW3。

下面是我用来下载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()
}

更新

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

我添加了这个函数,它可以存储暂停时的 destinationURL,并且可以在还原待定函数中使用。

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 asset函数中,我把下面一行从。

  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)

我想是设置最低要求导致了这个问题。

任何帮助或指导如何在退出应用程序时恢复待下载将是惊人的。谢谢你的帮助

ios swift avplayer m3u8 avassetdownloadtask
1个回答
0
投票

从HLSCatalog的例子中,我试着在创建AVAssetDownloadTask时将[AVAssetDownloadTaskMinimumRequiredMediaBitrateKey: 265_000]添加到选项中。

在AppDelegate中,我调用 restorePending(),getAllTasks返回待处理任务的数量。在下一步,我仍然得到错误,我还在解决,但任务不是空的。

希望对你有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.