public interface AdsLoader

Known direct subclasses
HlsInterstitialsAdsLoader

An ads loader that reads interstitials from the HLS playlist, adds them to the AdPlaybackState and passes the ad playback state to onAdPlaybackState.

ImaAdsLoader

AdsLoader using the IMA SDK.


Interface for loaders of ads, which can be used with AdsMediaSource.

Ads loaders notify the AdsMediaSource about events via EventListener. In particular, implementations must call onAdPlaybackState with a new copy of the current AdPlaybackState whenever further information about ads becomes known (for example, when an ad media URI is available, or an ad has played to the end).

start will be called when an ads media source first initializes, at which point the loader can request ads. If the player enters the background, stop will be called. Loaders should maintain any ad playback state in preparation for a later call to start. If an ad is playing when the player is detached, update the ad playback state with the current playback position using withAdResumePositionUs.

If onAdPlaybackState has been called, the implementation of start should invoke the same listener to provide the existing playback state to the new player.

Summary

Nested types

Listener for ads loader events.

public interface AdsLoader.Provider

Provides AdsLoader instances for media items that have ad tag URIs.

Public methods

default boolean
@UnstableApi
handleContentTimelineChanged(
    AdsMediaSource adsMediaSource,
    Timeline timeline
)

Notifies the ads loader when the content source has changed its timeline.

abstract void
@UnstableApi
handlePrepareComplete(
    AdsMediaSource adsMediaSource,
    int adGroupIndex,
    int adIndexInAdGroup
)

Notifies the ads loader that preparation of an ad media period is complete.

abstract void
@UnstableApi
handlePrepareError(
    AdsMediaSource adsMediaSource,
    int adGroupIndex,
    int adIndexInAdGroup,
    IOException exception
)

Notifies the ads loader that the player was not able to prepare media for a given ad.

abstract void

Releases the loader.

abstract void

Sets the player that will play the loaded ads.

abstract void

Sets the supported content types for ad media.

abstract void
@UnstableApi
start(
    AdsMediaSource adsMediaSource,
    DataSpec adTagDataSpec,
    Object adsId,
    AdViewProvider adViewProvider,
    AdsLoader.EventListener eventListener
)

Starts using the ads loader for playback.

abstract void
@UnstableApi
stop(
    AdsMediaSource adsMediaSource,
    AdsLoader.EventListener eventListener
)

Stops using the ads loader for playback and deregisters the event listener.

Public methods

handleContentTimelineChanged

@UnstableApi
default boolean handleContentTimelineChanged(
    AdsMediaSource adsMediaSource,
    Timeline timeline
)

Notifies the ads loader when the content source has changed its timeline. Called on the main thread by AdsMediaSource.

The default implementation returns false which makes the content timeline immediately being reported to the player.

When overriding this method for the purpose of reading ad data from the timeline to populate the AdPlaybackState with, false needs to be passed to the constructor of AdsMediaSource to indicate that the content source needs to be prepared upfront. This way an ads loader can defer calling onAdPlaybackState until the ad data from the timeline is available and populate the initial ad playback state with that data before publishing.

For live streams, this method is called additional times when the content source reports an advancing live window with new available media and/or new ad data in the manifest. If in such a case, the ads loader as a result calls onAdPlaybackState, true must be returned. This prevents the timeline being reported with stale ad data. Conversely, when the ad playback state is not passed into onAdPlaybackState, false must be returned to not drop a timeline update that needs to be published to the player.

Generally, if the timeline is not required to populate the ad playback state, start should be used to initiate loading ad data and publish the first ad playback state as early as possible. This method can still be overridden for informational or other purpose. In this case, false is returned here and the AdsMediaSource is used with lazy preparation enabled.

Parameters
AdsMediaSource adsMediaSource

The ads media source for which the content timeline changed.

Timeline timeline

The timeline of the content source.

Returns
boolean

true If onAdPlaybackState is or will be called, false otherwise.

handlePrepareComplete

@UnstableApi
abstract void handlePrepareComplete(
    AdsMediaSource adsMediaSource,
    int adGroupIndex,
    int adIndexInAdGroup
)

Notifies the ads loader that preparation of an ad media period is complete. Called on the main thread by AdsMediaSource.

Parameters
AdsMediaSource adsMediaSource

The ads media source for which preparation of ad media completed.

int adGroupIndex

The index of the ad group.

int adIndexInAdGroup

The index of the ad in the ad group.

handlePrepareError

@UnstableApi
abstract void handlePrepareError(
    AdsMediaSource adsMediaSource,
    int adGroupIndex,
    int adIndexInAdGroup,
    IOException exception
)

Notifies the ads loader that the player was not able to prepare media for a given ad. Implementations should update the ad playback state as the specified ad has failed to load. Called on the main thread by AdsMediaSource.

Parameters
AdsMediaSource adsMediaSource

The ads media source for which preparation of ad media failed.

int adGroupIndex

The index of the ad group.

int adIndexInAdGroup

The index of the ad in the ad group.

IOException exception

The preparation error.

release

abstract void release()

Releases the loader. Must be called by the application on the main thread when the instance is no longer needed.

setPlayer

abstract void setPlayer(@Nullable Player player)

Sets the player that will play the loaded ads.

This method must be called before the player is prepared with media using this ads loader.

This method must also be called on the main thread and only players which are accessed on the main thread are supported (player.getApplicationLooper() == Looper.getMainLooper()).

Parameters
@Nullable Player player

The player instance that will play the loaded ads. May be null to delete the reference to a previously set player.

setSupportedContentTypes

@UnstableApi
abstract void setSupportedContentTypes(@C.ContentType int[] contentTypes)

Sets the supported content types for ad media. Must be called before the first call to start. Subsequent calls may be ignored. Called on the main thread by AdsMediaSource.

Parameters
@C.ContentType int[] contentTypes

The supported content types for ad media. Each element must be one of CONTENT_TYPE_DASH, CONTENT_TYPE_HLS, CONTENT_TYPE_SS and CONTENT_TYPE_OTHER.

start

@UnstableApi
abstract void start(
    AdsMediaSource adsMediaSource,
    DataSpec adTagDataSpec,
    Object adsId,
    AdViewProvider adViewProvider,
    AdsLoader.EventListener eventListener
)

Starts using the ads loader for playback. Called on the main thread by AdsMediaSource.

Requests the ads loader to start loading ad data from the provided adTagDataSpec. Publishing an initial AdPlaybackState to provided eventListener is required to start playback. In the case of a pre roll, this ensures that the player doesn't briefly start playing content before ad data is available.

Parameters
AdsMediaSource adsMediaSource

The ads media source requesting to start loading ads.

DataSpec adTagDataSpec

A data spec for the ad tag to load.

Object adsId

An opaque identifier for the ad playback state across start/stop calls.

AdViewProvider adViewProvider

Provider of views for the ad UI.

AdsLoader.EventListener eventListener

Listener for ads loader events.

stop

@UnstableApi
abstract void stop(
    AdsMediaSource adsMediaSource,
    AdsLoader.EventListener eventListener
)

Stops using the ads loader for playback and deregisters the event listener. Called on the main thread by AdsMediaSource.

Parameters
AdsMediaSource adsMediaSource

The ads media source requesting to stop loading/playing ads.

AdsLoader.EventListener eventListener

The ads media source's listener for ads loader events.