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

时间:2016-11-16 12:08:12

标签: swift avaudioplayer audio-streaming

我有这个代码,应该下载音频流,按下暂停下载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")
}

但是当我启动应用时,它已经显示了" Done"。 我正在使用无线电流网址。

1 个答案:

答案 0 :(得分:2)

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

或者,如果您要取消下载,可以拨打downloadTask.cancel()