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

AVAssetDownloadDelegate methods for HLS getting called before I pause AVplayer

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

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")
}

但是当我启动应用程序时,它已经显示 "Done"。 我正在使用广播流 URL.

AVAssetDownloadURLSession 用于下载资产。如果你只是想玩它,你的 AVPlayer 可以处理。

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