HlsInterstitialsAdsLoader


@UnstableApi
public final class HlsInterstitialsAdsLoader implements AdsLoader


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

An ads ID must be unique within the playlist of ExoPlayer. If this is the case, a single HlsInterstitialsAdsLoader instance can be passed to multiple ads media sources. These ad media source can be added to the same playlist as far as each of the sources have a different ads IDs.

Summary

Nested types

A MediaSource.Factory to create a media source to play HLS streams with interstitials.

A listener to be notified of events emitted by the ads loader.

Public constructors

Creates an instance.

Public methods

void

Adds a Listener.

void
handleContentTimelineChanged(
    AdsMediaSource adsMediaSource,
    Timeline timeline
)

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

void
handlePrepareComplete(
    AdsMediaSource adsMediaSource,
    int adGroupIndex,
    int adIndexInAdGroup
)

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

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.

void

Releases the loader.

void

Removes a Listener.

void

Sets the player that will play the loaded ads.

void

Sets the supported content types for ad media.

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

Starts using the ads loader for playback.

void
stop(
    AdsMediaSource adsMediaSource,
    AdsLoader.EventListener eventListener
)

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

Public constructors

HlsInterstitialsAdsLoader

public HlsInterstitialsAdsLoader()

Creates an instance.

Public methods

addListener

public void addListener(HlsInterstitialsAdsLoader.Listener listener)

Adds a Listener.

handleContentTimelineChanged

public void handleContentTimelineChanged(
    AdsMediaSource adsMediaSource,
    Timeline timeline
)

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

If you override this callback for the purpose of reading ad data from the timeline to populate the AdPlaybackState with, you need to pass true to the constructor of AdsMediaSource to indicate the content source needs to be prepared upfront.

Parameters
AdsMediaSource adsMediaSource

The ads media source for which the content timeline changed.

Timeline timeline

The timeline of the content source.

handlePrepareComplete

public 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

public 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

public void release()

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

removeListener

public void removeListener(HlsInterstitialsAdsLoader.Listener listener)

Removes a Listener.

setPlayer

public 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()).

Throws
java.lang.IllegalStateException

If an app is attempting to set a new player instance after release was called or while ads media sources started by the old player are still active, an IllegalStateException is thrown. Release the old player first, or remove all ads media sources from it before setting another player instance.

setSupportedContentTypes

public 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

public 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.

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

public 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.