0
我一直在尝试使用AVAssetDownloadDelegate从网址下载视频。我能够从响应中播放视频,但我无法使用下面的代码下载媒体。NSURLErrorDomain错误-1当试图下载http Live流视频
我收到此错误“的操作无法完成。(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")
}
}
请看看这个..
嗨@noc,我实现了该函数,并可以看到委托从未被调用。我也指定了比特率,但仍然没有发现变化。我将尝试修改示例应用中的代码,以查看它是否能够正常工作并尽快回复。谢谢 –
哦,我忘了问你了。确保你没有在模拟器上运行。 HLS下载在模拟器上不起作用。 – noc