Search code examples
iosswiftcaching

How to cache AVPlayer video?


I have AVPlayers embedded into UITableViewCells and need a way to cache the video that's loaded so that when I refresh the table view, it doesn't have to load the videos again. The code I'm using at the moment to load videos is as follows:

let videoString = "http://www.someurl.com"
let videoURL = NSURL(string: videoString)

videoPlayer = AVPlayer(URL: videoURL!)
videoPlayer!.actionAtItemEnd = AVPlayerActionAtItemEnd.None

videoView.playerLayer.player = videoPlayer

I cache images by storing the returned image from a server in a dictionary with it's ID as the key. I'm looking for a similar way for videos, any help would be appreciated.


Solution

  • There is no built-in support to cache videos. Depending upon the size of the video you may try to download it in the background, once the video start playing.

    However do note that this process will increase the cache size of the app, and may consume a lot of space depending upon the size of your videos, so you may want to cache only a few videos that the user has watched recently, or you may restrict it to the 5-10 total videos.