使用AVAssetDownloadTask下载使我在URLSession上出现错误-iOS 10.x

时间:2018-09-05 11:45:51

标签: ios nsurlsession hls avassetdownloadtask

我以Apple Media Playback Programming Guide为例。

以下代码在iOS 11上有效,但在iOS 10上崩溃。

我按照以下步骤设置下载:

func setupAssetDownload(for item: DownloadItem) {
    let url = URL(string: item.urlVideo!)!
    let asset = AVURLAsset(url: url)

    // Create new AVAssetDownloadTask for the desired asset
    guard let task = downloadSession.makeAssetDownloadTask(asset: asset,
                                                           assetTitle: item.title!,
                                                           assetArtworkData: nil,
                                                           options: [AVAssetDownloadTaskMinimumRequiredMediaBitrateKey: 265_000])
        else { return }

    // Start task and begin download
    task.resume()
}

在这里失败:

func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
    /*
     This is the ideal place to begin downloading additional media selections
     once the asset itself has finished downloading.
     */
    if let error = error as NSError? {
        switch (error.domain, error.code) {
        case (NSURLErrorDomain, NSURLErrorCancelled):
            /*
             This task was canceled, you should perform cleanup using the
             URL saved from AVAssetDownloadDelegate.urlSession(_:assetDownloadTask:didFinishDownloadingTo:).
             */
            ...

        case (NSURLErrorDomain, NSURLErrorUnknown):
            fatalError("Downloading HLS streams is not supported in the simulator.")

        default:
            fatalError("An unexpected error occured \n\(error.code) - \(error.domain) - \(error.localizedDescription)")
        }
    } else {
        // downloaded
    }
}

我收到以下错误:

Error Domain=AVFoundationErrorDomain 
Code=-11800 \"The operation could not be completed\" 
UserInfo={
    NSLocalizedDescription=The operation could not be completed,
    NSLocalizedFailureReason=An unknown error occurred (-12780)
}

有人可以指出我的解决方法吗?

注意:我读到一些有关使用fileURLWithPath而不是string来组成URL的信息,但是根本没有用。

0 个答案:

没有答案