How to stream a video with AVURLAsset and save to disk the cached data

人走茶凉 提交于 2019-11-28 18:29:42

The solution for this problem is to use AVAssetExportSession and AVAssetResourceLoaderDelegate:

First step is to add a notification to know when the video finish. Then we can start saving it to disk.

override func viewDidLoad() {

    super.viewDidLoad()

    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(playerItemDidReachEnd(_:)), name: AVPlayerItemDidPlayToEndTimeNotification, object: nil)

    ...
}

deinit {

    NSNotificationCenter.defaultCenter().removeObserver(self)
}

The implementation of our function:

func playerItemDidReachEnd(notification: NSNotification) {

    if notification.object as? AVPlayerItem  == player.currentItem {

        let exporter = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetHighestQuality)

        let filename = "filename.mp4"

        let documentsDirectory = NSFileManager.defaultManager().URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask).last!

        let outputURL = documentsDirectory.URLByAppendingPathComponent(filename)

        exporter?.outputURL = outputURL
        exporter?.outputFileType = AVFileTypeMPEG4

        exporter?.exportAsynchronouslyWithCompletionHandler({

            print(exporter?.status.rawValue)
            print(exporter?.error)
        })
    }
}

Finally we need to make our AVURLAsset delegate of AVAssetResourceLoaderDelegate:

lazy var asset: AVURLAsset = {

    var asset: AVURLAsset = AVURLAsset(URL: self.url)

    asset.resourceLoader.setDelegate(self, queue: dispatch_get_main_queue())

    return asset
}()

And:

extension ViewController : AVAssetResourceLoaderDelegate {

}

I created a small demo with this code in GitHub.

The team at Calm has open-sourced our implementation to this. It's available as a CocoaPod. It's called PersistentStreamPlayer.

Features include:

  • streaming of audio file, starting playback as soon as first data is available
  • also saves streamed data to a file URL as soon as the buffer completes exposes timeBuffered, helpful for displaying buffer progress bars in the UI
  • handles re-starting the audio file after the buffer stream stalls (e.g. slow network)
  • simple play, pause and destroy methods (destroy clears all memory resources)
  • does not keep audio file data in memory, so that it supports large files that don't fit in RAM

You can find it here: https://github.com/calmcom/PersistentStreamPlayer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!