Preload and precache videos
Preload and precache upcoming playlist items in the FastPix iOS Player to start the next video instantly.
The FastPix iOS Player SDK can prepare upcoming playlist items ahead of time, so a viewer who advances to the next video sees little or no buffering. It offers two complementary mechanisms:
- Preloading initializes
AVPlayerIteminstances for upcoming videos in the background using a shadowAVPlayer. This warms up AVFoundation’s URL session cache, so when the SDK loads the same stream URL, the initial buffering is already complete. - Precaching downloads and stores HLS segments to disk, so the content is served from the local cache on later playback requests. DRM-protected items (those with a
drmToken) are skipped during precaching, because their segments are encrypted and can’t be cached.
Both managers expose delegate callbacks, so your app can react to preload and cache state changes in the UI.
Before you begin
Make sure you have the following:
- The FastPix iOS Player SDK installed in your project. See Install FastPix iOS player.
- A playlist added to the player. See Manage playlists.
How preloading and precaching differ
Set up the managers
Initialize both managers and assign their delegates before playback starts. Start preloading and precaching once the playlist is ready.
Preload upcoming videos
Preload the next items after the currently playing index. This example preloads the next two items, and you can change how many to preload. Call this method whenever the playlist position changes, such as after next(), previous(), jumpTo(), or a FastPixPlaylistStateChanged notification.
buildPlaybackURL(for:) is your own helper that builds the stream URL for a playlist item, using the same URL you’d use for playback.
Precache upcoming videos
Precache the HLS segments for the current and next video. DRM-protected items are skipped automatically.
Consume a preloaded item before navigating
When you move to the next item, call consumePreloadedItem(for:) before you call next(). This detaches the shadow player, so the SDK can reuse AVFoundation’s URL session cache when it loads the same stream URL.
Re-trigger after playlist changes
Re-trigger preloading and precaching inside the FastPixPlaylistStateChanged observer, so the window stays ahead of the current position.
Clean up
Stop all in-flight preload and precache tasks when the view controller is deallocated.
Handle preload events
Conform to PreloadManagerDelegate to receive preload lifecycle callbacks.
Handle precache events
Conform to PrecacheManagerDelegate to observe whether segments are served from disk or fetched from the network.
Best practices
- Call
preloadNextVideos()andprecacheUpcomingVideos()after every playlist navigation event (next(),previous(),jumpTo()), and insideplaylistStateChanged, to keep the preload window current. - DRM-protected items (where
drmTokenis non-empty) are excluded from precaching automatically. Preloading still applies to DRM items, because AVFoundation handles license fetching separately. - Always call
consumePreloadedItem(for:)beforenext(), to hand off the buffered data to AVFoundation’s URL session cache. - Call
preloadManager.clearAll()andprecacheManager.stopAllPrecaching()indeinit, to avoid memory leaks and dangling background tasks.
Frequently asked questions
What's the difference between preloading and precaching?
Preloading warms AVFoundation’s buffer for an upcoming item using a background shadow AVPlayer, so the next video starts instantly. Precaching downloads the HLS segments to disk, so later playback of the same item is served from the local cache.
Do preloading and precaching work with DRM-protected videos?
Preloading works with DRM items, because AVFoundation handles license fetching separately. Precaching skips DRM items automatically, because their segments are encrypted and can’t be cached.
How many upcoming items should I preload?
The examples preload and precache the next two items, which balances instant navigation against bandwidth and storage. Adjust the prefix(2) count to preload more or fewer items.
When should I trigger preloading and precaching?
Call preloadNextVideos() and precacheUpcomingVideos() after every navigation event (next(), previous(), jumpTo()) and inside the FastPixPlaylistStateChanged observer, so the window stays ahead of the current position.
How do I avoid memory leaks from preloading?
Call preloadManager.clearAll() and precacheManager.stopAllPrecaching() in deinit to stop all in-flight tasks and release resources.
What’s next
- Manage playlists to set up and navigate multiple videos.
- Handle network changes to keep playback stable across connectivity changes.
- Install FastPix iOS player if you haven’t set up the SDK yet.