How to stream a video with AVURLAsset and save to disk the cached data
<p>Some days ago I was asked to check how difficult is to play a video while downloading it from Internet. I know it's an easy task because someone told me a while ago. So, I checked and it was super easy.</p>

<p>The problem was that I wanted to save to disk the video to do not force the user to download it again and again.</p>

<p>The problem was to access the buffer and store it to disk. </p>

<p>Many answers in Stackoverflow says it is nor possible. Specially with videos.</p>

<p>My original code to play the video:</p>

<pre><code>import AVFoundation

....

//MARK: - Accessors

lazy var player: AVPlayer = {

    var player: AVPlayer = AVPlayer(playerItem: self.playerItem)

    player.actionAtItemEnd = AVPlayerActionAtItemEnd.None

    return player
}()

lazy var playerItem: AVPlayerItem = {

    var playerItem: AVPlayerItem = AVPlayerItem(asset: self.asset)

    return playerItem
}()

lazy var asset: AVURLAsset = {

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

    return asset
}()

lazy var playerLayer: AVPlayerLayer = {

    var playerLayer: AVPlayerLayer = AVPlayerLayer(player: self.player)

    playerLayer.frame = UIScreen.mainScreen().bounds
    playerLayer.backgroundColor = UIColor.clearColor().CGColor

    return playerLayer
}()

var url: NSURL = {

    var url = NSURL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")

    return url!
}()

//MARK: - ViewLifeCycle

override func viewDidLoad() {

    super.viewDidLoad()

    view.layer.addSublayer(playerLayer)

    player.play()
}
</code></pre>