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
- Your app creates a
PrerollAdPlayer, wraps it in a composable player and starts playback. - The Pre-Roll plays immediately (a prefetched ad) or as soon as it is received (an on-demand ad).
- In parallel, your app builds the SSAI player and gives it to the
PrerollAdPlayerwithattachContentPlayer(), or reports a failure withfailContentPlayerAttach(). - 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:
| Mode | Description |
|---|---|
WithPrefetchedAd | Plays 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 withQPAdsLoader.initAdsLoader(), see Pre-roll prefetch.WithoutPrefetchedAddoes 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
)
| Parameter | Type | Description |
|---|---|---|
| context | Context | Application or Activity context. |
| prerollAdRequestMode | PrerollAdRequestMode | WithPrefetchedAd or WithoutPrefetchedAd(adRequest, companionPresenter). See Choose how the Pre-Roll is requested. |
| adFrequencyControl | AdFrequencyControlConfiguration? | Optional. Applies ad frequency control to the Pre-Roll only. Configure AFC for the SSAI player on its own builder. |
| adIncomingLeadTimeMs | Long | Optional. Lead time for the ad incoming notification. |
| contentPlayerAttachTimeoutMs | Long | Optional. 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
adPlayerViewsis 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
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.
| API | Description |
|---|---|
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.
| Property | Value |
|---|---|
| Error code | 0x101003 (0x1003 plus the internal category bit ERROR_CATEGORY_MASK_INTERNAL) |
| Component code | 0x1003 (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. Apause()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
createPrerollAdPlayeronly 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,captionSettingsSelectorandplaybackPropertiesof 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 anIMAPlayer. Do not cast it toIMAPlayer.