mark
mark

Reputation: 45

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.

Upvotes: 3

Views: 1051

Answers (1)

Patrick Tescher
Patrick Tescher

Reputation: 3447

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()

Upvotes: 2

Related Questions