NSURLErrorDomain error -1 尝试下载 http Live Streaming Video 时
NSURLErrorDomain error -1 When trying to download http Live Streaming Video
我一直在尝试使用 AVAssetDownloadDelegate 从 url 下载视频。我可以播放响应中的视频,但无法使用以下代码下载媒体。
我收到此错误 " 操作无法完成。(NSURLErrorDomain 错误 -1。)"
这是我使用的代码
@IBOutlet weak var view1: UIView!
var configuration : URLSessionConfiguration? = nil
var downloadSession : AVAssetDownloadURLSession? = nil
override func viewDidLoad() {
super.viewDidLoad()
configuration = URLSessionConfiguration.background(withIdentifier: "downloadIdentifier")
downloadSession = AVAssetDownloadURLSession(configuration: configuration!,
assetDownloadDelegate: self,
delegateQueue: nil)
let url = URL(string: "http://sample.vodobox.net/skate_phantom_flex_4k/skate_phantom_flex_4k.m3u8")
let asset = AVURLAsset(url: url!)
let downloadTask = downloadSession?.makeAssetDownloadTask(asset: asset,
assetTitle: "assetTitle",
assetArtworkData: nil,
options: nil)
// Start task and begin download
downloadTask?.resume()
let playerItem = AVPlayerItem(asset: (downloadTask?.urlAsset)!)
let player = AVPlayer(playerItem: playerItem)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = view1.bounds
view1.layer.addSublayer(playerLayer)
player.play()
}
这是我实现的委托
func urlSession(_ session: URLSession, assetDownloadTask: AVAssetDownloadTask, didFinishDownloadingTo location: URL) {
print("Downloaded to Location : \(location)")
}
func urlSession(_ session: URLSession, assetDownloadTask: AVAssetDownloadTask, didLoad timeRange: CMTimeRange, totalTimeRangesLoaded loadedTimeRanges: [NSValue], timeRangeExpectedToLoad: CMTimeRange) {
var percentComplete = 0.0
// Iterate through the loaded time ranges
for value in loadedTimeRanges {
// Unwrap the CMTimeRange from the NSValue
let loadedTimeRange = value.timeRangeValue
// Calculate the percentage of the total expected asset duration
percentComplete += loadedTimeRange.duration.seconds / timeRangeExpectedToLoad.duration.seconds
}
print(percentComplete *= 100)
// Update UI state: post notification, update KVO state, invoke callback, etc.
}
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
if (error != nil){
print(error?.localizedDescription)
}else{
print("Error")
}
}
请看这个..
此 HLS 下载 API 在很多方面都存在问题。
您可以尝试的几件事:
- 在 makeAssetDownloadTask 调用中指定比特率(选项参数)
- 修改他们的示例应用程序以下载您的视频,这可以帮助您进行调试,看看您的播放列表是否有问题。
- 实施 func urlSession(_ session: URLSession, assetDownloadTask: AVAssetDownloadTask, didResolve resolvedMediaSelection: AVMediaSelection) 委托查看它是否被调用。
我一直在尝试使用 AVAssetDownloadDelegate 从 url 下载视频。我可以播放响应中的视频,但无法使用以下代码下载媒体。
我收到此错误 " 操作无法完成。(NSURLErrorDomain 错误 -1。)"
这是我使用的代码
@IBOutlet weak var view1: UIView!
var configuration : URLSessionConfiguration? = nil
var downloadSession : AVAssetDownloadURLSession? = nil
override func viewDidLoad() {
super.viewDidLoad()
configuration = URLSessionConfiguration.background(withIdentifier: "downloadIdentifier")
downloadSession = AVAssetDownloadURLSession(configuration: configuration!,
assetDownloadDelegate: self,
delegateQueue: nil)
let url = URL(string: "http://sample.vodobox.net/skate_phantom_flex_4k/skate_phantom_flex_4k.m3u8")
let asset = AVURLAsset(url: url!)
let downloadTask = downloadSession?.makeAssetDownloadTask(asset: asset,
assetTitle: "assetTitle",
assetArtworkData: nil,
options: nil)
// Start task and begin download
downloadTask?.resume()
let playerItem = AVPlayerItem(asset: (downloadTask?.urlAsset)!)
let player = AVPlayer(playerItem: playerItem)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = view1.bounds
view1.layer.addSublayer(playerLayer)
player.play()
}
这是我实现的委托
func urlSession(_ session: URLSession, assetDownloadTask: AVAssetDownloadTask, didFinishDownloadingTo location: URL) {
print("Downloaded to Location : \(location)")
}
func urlSession(_ session: URLSession, assetDownloadTask: AVAssetDownloadTask, didLoad timeRange: CMTimeRange, totalTimeRangesLoaded loadedTimeRanges: [NSValue], timeRangeExpectedToLoad: CMTimeRange) {
var percentComplete = 0.0
// Iterate through the loaded time ranges
for value in loadedTimeRanges {
// Unwrap the CMTimeRange from the NSValue
let loadedTimeRange = value.timeRangeValue
// Calculate the percentage of the total expected asset duration
percentComplete += loadedTimeRange.duration.seconds / timeRangeExpectedToLoad.duration.seconds
}
print(percentComplete *= 100)
// Update UI state: post notification, update KVO state, invoke callback, etc.
}
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
if (error != nil){
print(error?.localizedDescription)
}else{
print("Error")
}
}
请看这个..
此 HLS 下载 API 在很多方面都存在问题。 您可以尝试的几件事:
- 在 makeAssetDownloadTask 调用中指定比特率(选项参数)
- 修改他们的示例应用程序以下载您的视频,这可以帮助您进行调试,看看您的播放列表是否有问题。
- 实施 func urlSession(_ session: URLSession, assetDownloadTask: AVAssetDownloadTask, didResolve resolvedMediaSelection: AVMediaSelection) 委托查看它是否被调用。