Skip to main content

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
)
)
SettingDefaultDescription
modePreloadMode.RAMRAM keeps the prepared source in memory. DISK keeps a bounded segment cache shared by the process.
preloadDurationSeconds5How 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. Set preload to false when playing non prefetched content. When it is not set, the property is omitted from the request. If you construct a request with ContentAuthorizationRequest.Builder, use preload(true) to set the flag. See Content authorization.
  • Heartbeat: set reserveStreamOnPlayerLoad = true in HeartbeatFactory.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
)
  • priority is PreloadPriority.LOW by default. Use HIGH for the item the user is most likely to play.
  • configuration overrides the defaults for this prefetch only. For example, PreloadConfiguration(mode = PreloadMode.DISK, preloadDurationSeconds = 10).
  • startPositionMs defaults to the builder's initialStartTimeMs. Set it to null to use that value.
  • Set drmDelegate on the builder, in preload(). Do not assign drmDelegate on 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.

StateMeaning
QUEUEDWaiting for its turn.
PRELOADINGFetching and preparing media.
READYPreparation finished and the handle can be built.
LIMIT_REACHEDThe cache budget is full, so this prefetch was not kept.
FAILEDPreparation 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.