Content Prefetch
Content prefetch prepares a VOD item before the user presses play, so playback can start from the prepared copy. Use it for titles the user is likely to watch next, such as the next episode.
A prefetch is not a download. It caches a few seconds of media from a start position in memory or on disk, so playback starts faster. It is not available offline, and it does not survive a process restart.
Set the defaults
Call PreloadManager.init() once per app session, before the first prefetch. It sets the default cache mode and duration for prefetches that do not pass their own configuration.
// Uses the SDK defaults (PreloadMode.RAM, 5 seconds)
PreloadManager.init()
// Or set your own defaults
PreloadManager.init(
PreloadConfiguration(
mode = PreloadMode.DISK,
preloadDurationSeconds = 5
)
)
| Setting | Default | Description |
|---|---|---|
mode | PreloadMode.RAM | RAM keeps the prepared source in memory. DISK keeps a bounded segment cache shared by the process. |
preloadDurationSeconds | 5 | How many seconds of media to cache from the start position. Must be greater than 0. Cached data can be less when the budget or the network limits it. |
Calling init() again replaces the defaults. It does not clear existing prefetches.
Changes required
A prefetch needs two changes:
- Content authorization: set
platformAsset.copy(preload = true)in the content authorization request. Setpreloadtofalsewhen playing non prefetched content. When it is not set, the property is omitted from the request. If you construct a request withContentAuthorizationRequest.Builder, usepreload(true)to set the flag. See Content authorization. - Heartbeat: set
reserveStreamOnPlayerLoad = trueinHeartbeatFactory.createHeartbeatManager. See Heartbeat.
Use PlayerBuilder(context) and set everything the playback will use. Pass the start position to preload(), not to build(), because the cached data is fetched from that position.
val preload = PlayerBuilder(applicationContext)
.mediaURL(contentURL)
.mediaType(MediaType.DASH)
.drmScheme(DRMScheme.NONE)
.drmLicenseURL("")
.preload(
priority = PreloadPriority.HIGH,
configuration = null, // null uses the defaults from PreloadManager.init()
startPositionMs = resumeMs // for example, the user's last watched position
)
priorityisPreloadPriority.LOWby default. UseHIGHfor the item the user is most likely to play.configurationoverrides the defaults for this prefetch only. For example,PreloadConfiguration(mode = PreloadMode.DISK, preloadDurationSeconds = 10).startPositionMsdefaults to the builder'sinitialStartTimeMs. Set it tonullto use that value.- Set
drmDelegateon the builder, inpreload(). Do not assigndrmDelegateon the player after it is built. A delegate assigned after build makes the SDK skip the prefetch.
You can change the priority later:
preload.setPriority(PreloadPriority.LOW)
Check the status(Optional)
status() returns a PreloadStatus, or null after the handle is discarded or built.
| State | Meaning |
|---|---|
QUEUED | Waiting for its turn. |
PRELOADING | Fetching and preparing media. |
READY | Preparation finished and the handle can be built. |
LIMIT_REACHED | The cache budget is full, so this prefetch was not kept. |
FAILED | Preparation failed. errorMessage describes the error when available. |
READY means preparation finished. It does not mean that no network is needed during playback.
Play from a prefetch
When the user starts playback, build the player from the handle:
val player: Player = preload.buildPlayer()
Discard a prefetch
Discard a prefetch you no longer need. This frees its cache. It is safe to call more than once.
preload.discard()
To discard all prefetches that have not been built, for example on logout or a large navigation change:
PreloadManager.clearPreloads()
clearPreloads() cancels pending work and discards prefetches that are not built yet. Players that were already built keep playing, and the defaults from init() stay in effect.