Skip to main content

Pre-roll CSAI ad with SSAI playback

The fl-player-ads-ima library can play an IMA CSAI Pre-Roll (prefetched or requested on demand) before an SSAI stream (for example MediaTailor or Google IMA DAI) starts. The Pre-Roll begins right away while your app creates the SSAI session and player in the background, so the viewer does not wait for the SSAI setup before seeing the first ad.

How it works​

  1. Your app creates a PrerollAdPlayer, wraps it in a composable player and starts playback.
  2. The Pre-Roll plays immediately (a prefetched ad) or as soon as it is received (an on-demand ad).
  3. In parallel, your app builds the SSAI player and gives it to the PrerollAdPlayer with attachContentPlayer(), or reports a failure with failContentPlayerAttach().
  4. When the Pre-Roll ends, the SSAI player takes over. If the Pre-Roll ends before the SSAI player is ready, the player shows a loading state until it is attached.

If the Pre-Roll is not available or fails, content starts as soon as the SSAI player is attached.

Choose how the Pre-Roll is requested​

PrerollAdRequestMode selects the source of the Pre-Roll:

ModeDescription
WithPrefetchedAdPlays the Pre-Roll prefetched with QPAdsLoader.initAdsLoader(). If there is no usable prefetch, the Pre-Roll is skipped.
WithoutPrefetchedAd(adRequest, companionPresenter)Requests the Pre-Roll on demand with the given CSAIAdsRequest. The prefetched Pre-Roll is not used or discarded, so it stays available for later playbacks. companionPresenter is optional.

Prerequisites​

  • For WithPrefetchedAd, prefetch must be initialized before playback with QPAdsLoader.initAdsLoader(), see Pre-roll prefetch. WithoutPrefetchedAd does not need it.
  • The SSAI player is built with its usual builder, see MediaTailor or Google IMA DAI.

Create the Pre-Roll player​

val prerollPlayer: PrerollAdPlayer = IMAAdsPlayerFactory.createPrerollAdPlayer(
context = context,
prerollAdRequestMode = PrerollAdRequestMode.WithPrefetchedAd,
adFrequencyControl = afcConfiguration, // optional
contentPlayerAttachTimeoutMs = 20_000L // optional
)
ParameterTypeDescription
contextContextApplication or Activity context.
prerollAdRequestModePrerollAdRequestModeWithPrefetchedAd or WithoutPrefetchedAd(adRequest, companionPresenter). See Choose how the Pre-Roll is requested.
adFrequencyControlAdFrequencyControlConfiguration?Optional. Applies ad frequency control to the Pre-Roll only. Configure AFC for the SSAI player on its own builder.
adIncomingLeadTimeMsLongOptional. Lead time for the ad incoming notification.
contentPlayerAttachTimeoutMsLongOptional. How long to wait for attachContentPlayer() or failContentPlayerAttach(). Default is 20000 (20 seconds).

On-demand Pre-Roll​

val prerollPlayer: PrerollAdPlayer = IMAAdsPlayerFactory.createPrerollAdPlayer(
context = context,
prerollAdRequestMode = PrerollAdRequestMode.WithoutPrefetchedAd(csaiAdsRequest),
adFrequencyControl = afcConfiguration // optional
)
  • The ad is requested while your app builds the SSAI player. The request starts once the ad view returned by adPlayerViews is attached to a window, so add the composable player's views to your layout as usual.
  • A request that gets no answer within 8 seconds, fails, or is rejected by AFC is skipped, and content starts as soon as the SSAI player is attached. A response that arrives after the timeout is discarded and never starts an ad. If the ad view is never attached, the Pre-Roll is skipped after 20 seconds.
  • The time spent on the ad request counts toward contentPlayerAttachTimeoutMs.
  • Each on-demand playback uses its own ad loader and view, which are released when the playback ends.

Companion ads​

Pass a CompanionPresentation to WithoutPrefetchedAd to show companion ads for the on-demand Pre-Roll:

val prerollPlayer = IMAAdsPlayerFactory.createPrerollAdPlayer(
context = context,
prerollAdRequestMode = PrerollAdRequestMode.WithoutPrefetchedAd(csaiAdsRequest, companionPresenter)
)

The factory prepares the presenter's slots before the ad is requested and binds the presenter to the returned player. Use a presenter that belongs to this playback, and do not reuse the mode object after the playback.

Wrap it with composablePlayerWith​

warning

Always wrap the PrerollAdPlayer with composablePlayerWith(prerollPlayer as Player) and use the returned composable player as your app's player. This is required: Heartbeat, bookmarks and the other platform features are attached to the composable player and do not work if you use the PrerollAdPlayer directly or wrap only the SSAI player.

val composablePlayer = composablePlayerWith(prerollPlayer as Player)
player = composablePlayer // the player your app, Heartbeat and bookmark code use

composablePlayer.attachRendererView(
playerRoot,
FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
)

Listeners and settings you apply before the SSAI player exists are kept and applied to it once attached: player listeners, ad listeners, volume, mute, playback rate, resize mode, DRM delegate and preferred MIME types. Only the values you explicitly set are applied; the SSAI player keeps its own defaults for the rest.

prerollPlayer.addListener(playerListener)
prerollPlayer.registerAdListener(adEventListener)
composablePlayer.drmDelegate = PlatformPlayer.defaultDRMDelegate(logger)

composablePlayer.play() // Pre-Roll starts immediately, SSAI content follows

Attach the SSAI player​

Build the SSAI player in a coroutine. Pass the result to the PrerollAdPlayer.

APIDescription
attachContentPlayer(player: AdComposedPlayer)Hands over the built SSAI player. If the Pre-Roll is still playing, the SSAI player starts after it ends. A second or late call stops the given player and is ignored.
failContentPlayerAttach(error: Error)Reports that the SSAI player could not be built. The error is delivered to the player listeners after the Pre-Roll ends, so a playing ad is not interrupted.

MediaTailor​

val prerollPlayer = IMAAdsPlayerFactory.createPrerollAdPlayer(
context,
PrerollAdRequestMode.WithPrefetchedAd // or WithoutPrefetchedAd(csaiAdsRequest)
)
prerollPlayer.addListener(stateObserver)
prerollPlayer.registerAdListener(stateObserver)

// Keep the wrapper of this request: `player` may be replaced by another playback.
val composablePlayer = composablePlayerWith(prerollPlayer as Player)
player = composablePlayer

scope.launch {
when (val result = mediaTailorAdsPlayerBuilder.buildAsync(context, isVodContent)) {
is Result.Success -> {
val mediaTailorPlayer = result.value
if (mediaTailorPlayer != null) {
prerollPlayer.attachContentPlayer(mediaTailorPlayer)
// Auxiliary listeners are not forwarded by the Pre-Roll player.
if (player === composablePlayer) {
composablePlayer.getComposedPlayer()?.addAuxiliaryListener(stateObserver)
}
} else {
handleInitializationFailure(composablePlayer)
}
}
is Result.Failure -> {
val error = result.value?.internalError ?: result.value
if (error != null) {
prerollPlayer.failContentPlayerAttach(error)
} else {
handleInitializationFailure(composablePlayer)
}
}
}
}

Google IMA DAI​

val prerollPlayer = IMAAdsPlayerFactory.createPrerollAdPlayer(
context,
PrerollAdRequestMode.WithPrefetchedAd // or WithoutPrefetchedAd(csaiAdsRequest)
)
prerollPlayer.registerAdListener(stateObserver)
val composablePlayer = composablePlayerWith(prerollPlayer as Player)
player = composablePlayer

scope.launch {
when (val result = imaDaiAdsPlayerBuilder.build(context, adRequest, sessionConfig)) {
is Result.Success -> {
val daiPlayer = result.value
if (daiPlayer != null) {
daiPlayer.drmDelegate = PlatformPlayer.defaultDRMDelegate(logger)
// Set the start position on the DAI player, the Pre-Roll player has no playback properties.
initialPositionMs?.let { daiPlayer.playbackProperties.initialStartTimeMs = it }
prerollPlayer.attachContentPlayer(daiPlayer)
} else {
handleInitializationFailure(composablePlayer)
}
}
is Result.Failure -> {
result.value?.let { prerollPlayer.failContentPlayerAttach(it) }
?: handleInitializationFailure(composablePlayer)
}
}
}

handleInitializationFailure is your app code: it should report an initialization error to the UI and stop the composable player when the build returned neither a player nor an error.

Attach timeout​

If neither attachContentPlayer() nor failContentPlayerAttach() is called within contentPlayerAttachTimeoutMs after the player is created, the Pre-Roll is aborted immediately and the player listeners receive onError with the following error. A player attached afterward is stopped and ignored.

PropertyValue
Error code0x101003 (0x1003 plus the internal category bit ERROR_CATEGORY_MASK_INTERNAL)
Component code0x1003 (AdErrorCode.CONTENT_PLAYER_ATTACH_TIMEOUT)

Choose a timeout that covers your SSAI session setup, including retries, plus the player build time.

Stop playback​

Call stop() on the composable player when the user leaves the screen. It stops the Pre-Roll and the attached SSAI player. If the SSAI player is still being built, it is stopped as soon as it is attached.

composablePlayer.stop()

Behavior notes​

  • Content starts only after play() was requested. A pause() while waiting for the SSAI player is respected.
  • Content playback state is not reported while the Pre-Roll is active; listeners see the Pre-Roll state until the SSAI player takes over.
  • Ad frequency control on createPrerollAdPlayer only gates and measures the Pre-Roll. Configure AFC for the SSAI content and its own ad breaks on the SSAI player builder; nothing is counted twice.
  • playerAdapter, captionSettingsSelector and playbackProperties of the Pre-Roll player are not available before the SSAI player is attached.
  • The prefetched Pre-Roll is never consumed or discarded by an on-demand playback.
  • The returned player is a PrerollAdPlayer, not an IMAPlayer. Do not cast it to IMAPlayer.