Package com.castlabs.android.adverts
Class HlsInterstitialAdLoader
java.lang.Object
com.castlabs.android.adverts.ServerAdLoader
com.castlabs.android.adverts.HlsInterstitialAdLoader
- All Implemented Interfaces:
AdLoader
AdLoader implementation for handling HLS interstitial ads using ExoPlayer's HlsInterstitialMediaSource.
This class manages ad scheduling, playback state, and ad group tracking for HLS streams.
-
Nested Class Summary
Nested classes/interfaces inherited from interface com.castlabs.android.adverts.AdLoader
AdLoader.Listener -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiongetAdApi()Get anAdApito issue custom operations to the ad providerlonggetAdPositionMs(Ad ad, com.google.android.exoplayer2.ExoPlayer player) Returns the position of the providedAdin milliseconds relative to the window that is currently playing (i.e.Get the currently loaded and prepared for playing AdvoidonContentMediaSource(PlayerConfig playerConfig, com.google.android.exoplayer2.source.MediaSource mediaSource) Notifies the loader of a contentMediaSourcethat has just been built and will be used for playback.voidonContentMediaSourceReleased(PlayerConfig playerConfig, com.google.android.exoplayer2.source.MediaSource mediaSource) Notifies the loader that a contentMediaSourcepreviously reported throughAdLoader.onContentMediaSource(PlayerConfig, MediaSource)has been retired: it is no longer mapped for its item and will never be prepared again.voidonPlayerStateChanged(boolean playWhenReady, int playbackState) Called when the player state is changedvoidonPositionDiscontinuity(int reason) Called when the playback discontinuity occurslongonSetPosition(long newPositionUs) Called just before thePlayerControllerstart seeking or setting a new position.voidrelease()Release any unused resourcesvoidscheduleAd(AdRequest adRequest) Start loading anAdRequest.voidsetPlayerController(PlayerController playerController) Set the activePlayerControllerinstance ornullif there is no activePlayerControlleror it is about to be releasedMethods inherited from class com.castlabs.android.adverts.ServerAdLoader
createMediaSource, getAdContainer, requiresMediaSourceHook
-
Constructor Details
-
HlsInterstitialAdLoader
public HlsInterstitialAdLoader()Default constructor for HlsInterstitialAdLoader.
-
-
Method Details
-
setPlayerController
Description copied from interface:AdLoaderSet the activePlayerControllerinstance ornullif there is no activePlayerControlleror it is about to be released- Specified by:
setPlayerControllerin interfaceAdLoader- Overrides:
setPlayerControllerin classServerAdLoader- Parameters:
playerController- ThePlayerControllerinstance ornull
-
getCurrentAd
Description copied from interface:AdLoaderGet the currently loaded and prepared for playing Ad- Returns:
- The current Ad or
null
-
release
public void release()Description copied from interface:AdLoaderRelease any unused resources- Specified by:
releasein interfaceAdLoader- Overrides:
releasein classServerAdLoader
-
onSetPosition
public long onSetPosition(long newPositionUs) Description copied from interface:AdLoaderCalled just before thePlayerControllerstart seeking or setting a new position. Can be used when the new position has to be adjusted by theAdLoaderimplementation e.g. to do ads snap back. If the adjustment is not needed then the provided position can simply be returned.- Parameters:
newPositionUs- The new position in microseconds- Returns:
- The adjusted position in microseconds
-
onPlayerStateChanged
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) Description copied from interface:AdLoaderCalled when the player state is changed- Parameters:
playWhenReady- Play when ready flagplaybackState- Playback state
-
onPositionDiscontinuity
public void onPositionDiscontinuity(int reason) Description copied from interface:AdLoaderCalled when the playback discontinuity occurs- Parameters:
reason- The reason for discontinuity
-
scheduleAd
Description copied from interface:AdLoaderStart loading anAdRequest. This may be also called during playback. Support for on-request ad scheduling depends on the AdLoader implementation.- Specified by:
scheduleAdin interfaceAdLoader- Overrides:
scheduleAdin classServerAdLoader- Parameters:
adRequest- The ad request to perform.
-
onContentMediaSource
public void onContentMediaSource(@NonNull PlayerConfig playerConfig, @NonNull com.google.android.exoplayer2.source.MediaSource mediaSource) Description copied from interface:AdLoaderNotifies the loader of a contentMediaSourcethat has just been built and will be used for playback. Called synchronously while the source is being created, before it is prepared, once per built source. A playlist rebuilds an item's media source whenever the item is evicted, refreshed, or removed and re-added, so onePlayerConfigcan produce a sequence of media sources over its lifetime and each one is reported here. State recorded against the configuration must therefore be keyed, or at least validated, by media-source instance;AdLoader.onContentMediaSourceReleased(PlayerConfig, MediaSource)reports the retirement of each one.- Specified by:
onContentMediaSourcein interfaceAdLoader- Overrides:
onContentMediaSourcein classServerAdLoader- Parameters:
playerConfig- ThePlayerConfigassociated with the media source.mediaSource- The content media source that will be prepared.
-
onContentMediaSourceReleased
public void onContentMediaSourceReleased(@NonNull PlayerConfig playerConfig, @NonNull com.google.android.exoplayer2.source.MediaSource mediaSource) Description copied from interface:AdLoaderNotifies the loader that a contentMediaSourcepreviously reported throughAdLoader.onContentMediaSource(PlayerConfig, MediaSource)has been retired: it is no longer mapped for its item and will never be prepared again. The player may still be draining media periods created from it, so this is not a signal that its resources are already gone, only that no further playback will come from it. The retirement is reported after the replacement generation, if any, has been reported throughAdLoader.onContentMediaSource(PlayerConfig, MediaSource)for the same configuration. An implementation keeping per-configuration state must therefore compare instances before discarding it, rather than keying the discard on the configuration alone:
State belonging to the playlist item rather than to one media source — a server-side ad session, for instance — should not be discarded here, since it survives a generation replacement. Bulk teardown is not reported here. Releasing the player controller detaches the loader throughif (sources.get(playerConfig) == mediaSource) { sources.remove(playerConfig); }AdLoader.setPlayerController(PlayerController)andAdLoader.release(), and any remaining per-configuration state must be dropped there.- Parameters:
playerConfig- ThePlayerConfigthe media source was built for. The same instance that was passed toAdLoader.onContentMediaSource(PlayerConfig, MediaSource).mediaSource- The content media source that has been retired.
-
getAdApi
Description copied from interface:AdLoaderGet anAdApito issue custom operations to the ad provider- Returns:
- the
AdApi
-
getAdPositionMs
public long getAdPositionMs(@NonNull Ad ad, @NonNull com.google.android.exoplayer2.ExoPlayer player) Description copied from interface:AdLoaderReturns the position of the providedAdin milliseconds relative to the window that is currently playing (i.e. the media period exposed byPlayer.getCurrentWindowIndex()). Implementations that operate on concatenated timelines must therefore normalize the ad offset to the active window instead of returning a global playlist position.- Parameters:
ad- The ad for which the position should be resolved.player- The player instance that provides the current window context.- Returns:
- The ad start position in milliseconds, relative to the currently playing window.
-