gpt4 book ai didi

swift - 在暂停 AVplayer 之前调用 HLS 的 AVAssetDownloadDelegate 方法

转载 作者:搜寻专家 更新时间:2023-10-31 22:44:21 26 4
gpt4 key购买 nike

我有这段代码可以下载音频流,按暂停键下载应该停止

func setupAssetDownload() {

let configuration = URLSessionConfiguration.background(withIdentifier: "downloadIdentifier")
let downloadSession=AVAssetDownloadURLSession(configuration: configuration, assetDownloadDelegate: self, delegateQueue: OperationQueue.main)
let url = NSURL(string: "http: ... stream url")
let asset = AVURLAsset(url: url as! URL)

if #available(iOS 10.0, *) {
let downloadTask = downloadSession.makeAssetDownloadTask(asset: asset,assetTitle: "downloadedAudio",assetArtworkData: nil,options: nil)
downloadTask?.resume()
let playerItem = AVPlayerItem(asset: (downloadTask?.urlAsset)!)
player = AVPlayer(playerItem: playerItem)
player.play()
} else {
// Fallback on earlier versions
}

}
func urlSession(_ session: URLSession, assetDownloadTask: AVAssetDownloadTask, didFinishDownloadingTo location: URL) {
UserDefaults.standard.set(location.relativePath, forKey: "assetPath")
print("Done")
}

但是当我启动应用程序时,它已经显示“完成”。我正在使用 radio 流 URL。

最佳答案

AVAssetDownloadURLSession 用于下载 Assets 。如果您只想播放它,AVPlayer 可以处理。

或者,如果您想取消下载,您可以调用 downloadTask.cancel()

关于swift - 在暂停 AVplayer 之前调用 HLS 的 AVAssetDownloadDelegate 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40631816/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com