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