JEL
JEL

Reputation: 1550

Cache videos in tableview

I want to cache videos that are displayed in a tableview. However, I am not sure what to cache. I'm using AVFoundation, in particular I'm using AVPlayer and creating AVPlayerItem's.

My question is: what do I cache? Is it the AVPlayer, AVPlayerItem, or the underlying asset of AVPlayerItem called the AVAsset?

Please give a code sample (or library) with answer. Thanks!

Upvotes: 0

Views: 1606

Answers (2)

Vlad Pulichev
Vlad Pulichev

Reputation: 3272

You need to have private var mediaCache = NSMutableDictionary() for caching videos.

How to cache:

let assetForCache = AVAsset(url: URL(string: cell.videoRef)!)
self.mediaCache.setObject(assetForCache, forKey: cacheKey as NSCopying)

How to use: Just check if object exists in cache for this row. If yes - use it, no - download it

Upvotes: 1

Vinodh
Vinodh

Reputation: 5268

Cache means download the video of each cell and try to use downloaded video for the second time . Either you can Temp location or Document directory. Execute a download call to download the video from URL Video Link.

In your cellForRowat indexpath check whether video available in your local if its there play.or else stream for the first time , simultaneously place a download call for that video in background .So that for second time you can use local video and play without internet .

Upvotes: 0

Related Questions