Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVAssetDownloadDelegate methods for HLS getting called before I pause AVplayer

i have this code that supposed to download the audio stream and by pressing pause the download shoud stop

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

but when I launch the app ,it already shows "Done". I'm using radio stream URL.

like image 567
mark Avatar asked Jan 30 '26 01:01

mark


1 Answers

AVAssetDownloadURLSession is for downloading the asset. If you just want to play it you AVPlayer can handle that.

Alternatively if you want to cancel the download you can call downloadTask.cancel()

like image 60
Patrick Tescher Avatar answered Jan 31 '26 16:01

Patrick Tescher



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!