尝试使用.m3u8下载一个AVAssetDownloadURLSession视频文件(http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8)。当我在Xcode中运行代码时,会得到一个错误:
“错误Domain=AVFoundationErrorDomain代码=-11800\”操作无法完成\“UserInfo={NSLocalizedFailureReason=An未知错误发生(-12780),NSLocalizedDescription=The操作无法完成}”
。我使用的代码:
import UIKit
import AVFoundation
class ViewController: UIViewController, AVAssetDownloadDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.setupAssetDownload()
// Do any additional setup after loading the view, typically from a nib.
}
func setupAssetDownload() {
// Create new background session configuration.
let configuration = URLSessionConfiguration.background(withIdentifier: "AssetID")
// Create a new AVAssetDownloadURLSession with background configuration, delegate, and queue
let downloadSession = AVAssetDownloadURLSession(configuration: configuration,
assetDownloadDelegate: self,
delegateQueue: OperationQueue.main)
let url = URL(string: "http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8")
// let url = URL(string: "https://qthttp.apple.com.edgesuite.net/1010qwoeiuryfg/sl.m3u8")
let asset = AVURLAsset(url: url!)
// Create new AVAssetDownloadTask for the desired asset
let downloadTask = downloadSession.makeAssetDownloadTask(asset: asset,
assetTitle: "AssetTitle",
assetArtworkData: nil,
options: nil)
// Start task and begin download
downloadTask?.resume()
}
//MARK: Delegates
public func urlSession(_ session: URLSession, assetDownloadTask: AVAssetDownloadTask, didFinishDownloadingTo location: URL){
print("DownloadedLocation:\(location.absoluteString)")
}
public func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
print("Error")
}
public func urlSession(_ session: URLSession, didBecomeInvalidWithError error: Error?) {
print("Error")
}
public func urlSession(_ session: URLSession, taskIsWaitingForConnectivity task: URLSessionTask) {
print("Waiting")
}
public func urlSession(_ session: URLSession, task: URLSessionTask, didFinishCollecting metrics: URLSessionTaskMetrics) {
print("Finihs collecting metrics:")
}
}发布于 2018-07-26 06:40:15
我检查了您的代码,发现您没有在. NSAllowsArbitraryLoads plist文件中添加NSAppTransportSecurity字典中的YES中的键。
试着分享结果。
发布于 2021-09-29 18:38:03
模拟器不支持下载HLS流。
你应该使用真正的设备。
https://stackoverflow.com/questions/51532165
复制相似问题