Class ImaAdsLoader
- java.lang.Object
-
- com.google.android.exoplayer2.ext.ima.ImaAdsLoader
-
- All Implemented Interfaces:
Player.EventListener,Player.Listener,AdsLoader
public final class ImaAdsLoader extends Object implements Player.Listener, AdsLoader
AdsLoaderusing the IMA SDK. All methods must be called on the main thread.The player instance that will play the loaded ads must be set before playback using
setPlayer(Player). If the ads loader is no longer required, it must be released by callingrelease().See https://developers.google.com/interactive-media-ads/docs/sdks/android/compatibility for information on compatible ad tag formats. Pass the ad tag URI when setting media item playback properties (if using the media item API) or as a
DataSpecwhen constructing theAdsMediaSource(if using media sources directly). For the latter case, please note that this implementation delegates loading of the data spec to the IMA SDK, so range and headers specifications will be ignored in ad tag URIs. Literal ads responses can be encoded as data scheme data specs, for example, by constructing the data spec using a URI generated viaUtil.getDataUriForString(String, String).The IMA SDK can report obstructions to the ad view for accurate viewability measurement. This means that any overlay views that obstruct the ad overlay but are essential for playback need to be registered via the
AdViewProviderpassed to theAdsMediaSource. See the IMA SDK Open Measurement documentation for more information.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classImaAdsLoader.BuilderBuilder forImaAdsLoader.-
Nested classes/interfaces inherited from interface com.google.android.exoplayer2.source.ads.AdsLoader
AdsLoader.EventListener
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidfocusSkipButton()Moves UI focus to the skip button (or other interactive elements), if currently shown.com.google.ads.interactivemedia.v3.api.AdDisplayContainergetAdDisplayContainer()Returns theAdDisplayContainerused by this loader, ornullif ads have not been requested yet.com.google.ads.interactivemedia.v3.api.AdsLoadergetAdsLoader()Returns the underlyingAdsLoaderwrapped by this instance, ornullif ads have not been requested yet.voidhandlePrepareComplete(AdsMediaSource adsMediaSource, int adGroupIndex, int adIndexInAdGroup)Notifies the ads loader that preparation of an ad media period is complete.voidhandlePrepareError(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.voidonPositionDiscontinuity(Player.PositionInfo oldPosition, Player.PositionInfo newPosition, @com.google.android.exoplayer2.Player.DiscontinuityReason int reason)Called when a position discontinuity occurs.voidonRepeatModeChanged(@com.google.android.exoplayer2.Player.RepeatMode int repeatMode)Called when the value ofPlayer.getRepeatMode()changes.voidonShuffleModeEnabledChanged(boolean shuffleModeEnabled)Called when the value ofPlayer.getShuffleModeEnabled()changes.voidonTimelineChanged(Timeline timeline, @com.google.android.exoplayer2.Player.TimelineChangeReason int reason)Called when the timeline has been refreshed.voidrelease()Releases the loader.voidrequestAds(DataSpec adTagDataSpec, Object adsId, ViewGroup adViewGroup)Requests ads, if they have not already been requested.voidsetPlayer(Player player)Sets the player that will play the loaded ads.voidsetSupportedContentTypes(int... contentTypes)Sets the supported content types for ad media.voidskipAd()Skips the current ad.voidstart(AdsMediaSource adsMediaSource, DataSpec adTagDataSpec, Object adsId, AdViewProvider adViewProvider, AdsLoader.EventListener eventListener)Starts using the ads loader for playback.voidstop(AdsMediaSource adsMediaSource, AdsLoader.EventListener eventListener)Stops using the ads loader for playback and deregisters the event listener.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.google.android.exoplayer2.Player.EventListener
onLoadingChanged, onMaxSeekToPreviousPositionChanged, onPlayerStateChanged, onPositionDiscontinuity, onSeekProcessed, onTracksChanged, onTrackSelectionParametersChanged
-
Methods inherited from interface com.google.android.exoplayer2.Player.Listener
onAudioAttributesChanged, onAudioSessionIdChanged, onAvailableCommandsChanged, onCues, onDeviceInfoChanged, onDeviceVolumeChanged, onEvents, onIsLoadingChanged, onIsPlayingChanged, onMediaItemTransition, onMediaMetadataChanged, onMetadata, onPlaybackParametersChanged, onPlaybackStateChanged, onPlaybackSuppressionReasonChanged, onPlayerError, onPlayerErrorChanged, onPlaylistMetadataChanged, onPlayWhenReadyChanged, onRenderedFirstFrame, onSeekBackIncrementChanged, onSeekForwardIncrementChanged, onSkipSilenceEnabledChanged, onSurfaceSizeChanged, onTracksInfoChanged, onVideoSizeChanged, onVolumeChanged
-
-
-
-
Method Detail
-
getAdsLoader
@Nullable public com.google.ads.interactivemedia.v3.api.AdsLoader getAdsLoader()
Returns the underlyingAdsLoaderwrapped by this instance, ornullif ads have not been requested yet.
-
getAdDisplayContainer
@Nullable public com.google.ads.interactivemedia.v3.api.AdDisplayContainer getAdDisplayContainer()
Returns theAdDisplayContainerused by this loader, ornullif ads have not been requested yet.Note: any video controls overlays registered via
BaseDisplayContainer.registerFriendlyObstruction(FriendlyObstruction)will be unregistered automatically when the media source detaches from this instance. It is therefore necessary to re-register views each time the ads loader is reused. Alternatively, provide overlay views via theAdViewProviderwhen creating the media source to benefit from automatic registration.
-
requestAds
public void requestAds(DataSpec adTagDataSpec, Object adsId, @Nullable ViewGroup adViewGroup)
Requests ads, if they have not already been requested. Must be called on the main thread.Ads will be requested automatically when the player is prepared if this method has not been called, so it is only necessary to call this method if you want to request ads before preparing the player.
- Parameters:
adTagDataSpec- The data specification of the ad tag to load. See class javadoc for information about compatible ad tag formats.adsId- A opaque identifier for the ad playback state across start/stop calls.adViewGroup- AViewGroupon top of the player that will show any ad UI, ornullif playing audio-only ads.
-
skipAd
public void skipAd()
Skips the current ad.This method is intended for apps that play audio-only ads and so need to provide their own UI for users to skip skippable ads. Apps showing video ads should not call this method, as the IMA SDK provides the UI to skip ads in the ad view group passed via
AdViewProvider.
-
focusSkipButton
public void focusSkipButton()
Moves UI focus to the skip button (or other interactive elements), if currently shown. SeeBaseManager.focus().
-
setPlayer
public void setPlayer(@Nullable Player player)Description copied from interface:AdsLoaderSets 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()).
-
setSupportedContentTypes
public void setSupportedContentTypes(@ContentType int... contentTypes)
Description copied from interface:AdsLoaderSets the supported content types for ad media. Must be called before the first call toAdsLoader.start(AdsMediaSource, DataSpec, Object, AdViewProvider, EventListener). Subsequent calls may be ignored. Called on the main thread byAdsMediaSource.- Specified by:
setSupportedContentTypesin interfaceAdsLoader- Parameters:
contentTypes- The supported content types for ad media. Each element must be one ofC.TYPE_DASH,C.TYPE_HLS,C.TYPE_SSandC.TYPE_OTHER.
-
start
public void start(AdsMediaSource adsMediaSource, DataSpec adTagDataSpec, Object adsId, AdViewProvider adViewProvider, AdsLoader.EventListener eventListener)
Description copied from interface:AdsLoaderStarts using the ads loader for playback. Called on the main thread byAdsMediaSource.- Specified by:
startin interfaceAdsLoader- Parameters:
adsMediaSource- The ads media source requesting to start loading ads.adTagDataSpec- A data spec for the ad tag to load.adsId- An opaque identifier for the ad playback state across start/stop calls.adViewProvider- Provider of views for the ad UI.eventListener- Listener for ads loader events.
-
stop
public void stop(AdsMediaSource adsMediaSource, AdsLoader.EventListener eventListener)
Description copied from interface:AdsLoaderStops using the ads loader for playback and deregisters the event listener. Called on the main thread byAdsMediaSource.
-
release
public void release()
Description copied from interface:AdsLoaderReleases the loader. Must be called by the application on the main thread when the instance is no longer needed.
-
handlePrepareComplete
public void handlePrepareComplete(AdsMediaSource adsMediaSource, int adGroupIndex, int adIndexInAdGroup)
Description copied from interface:AdsLoaderNotifies the ads loader that preparation of an ad media period is complete. Called on the main thread byAdsMediaSource.- Specified by:
handlePrepareCompletein interfaceAdsLoader- Parameters:
adsMediaSource- The ads media source for which preparation of ad media completed.adGroupIndex- The index of the ad group.adIndexInAdGroup- The index of the ad in the ad group.
-
handlePrepareError
public void handlePrepareError(AdsMediaSource adsMediaSource, int adGroupIndex, int adIndexInAdGroup, IOException exception)
Description copied from interface:AdsLoaderNotifies 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 byAdsMediaSource.- Specified by:
handlePrepareErrorin interfaceAdsLoader- Parameters:
adsMediaSource- The ads media source for which preparation of ad media failed.adGroupIndex- The index of the ad group.adIndexInAdGroup- The index of the ad in the ad group.exception- The preparation error.
-
onTimelineChanged
public void onTimelineChanged(Timeline timeline, @TimelineChangeReason @com.google.android.exoplayer2.Player.TimelineChangeReason int reason)
Description copied from interface:Player.EventListenerCalled when the timeline has been refreshed.Note that the current
MediaItemor playback position may change as a result of a timeline change. If playback can't continue smoothly because of this timeline change, a separatePlayer.EventListener.onPositionDiscontinuity(PositionInfo, PositionInfo, int)callback will be triggered.Player.EventListener.onEvents(Player, Events)will also be called to report this event along with other events that happen in the sameLoopermessage queue iteration.- Specified by:
onTimelineChangedin interfacePlayer.EventListener- Specified by:
onTimelineChangedin interfacePlayer.Listener- Parameters:
timeline- The latest timeline. Never null, but may be empty.reason- ThePlayer.TimelineChangeReasonresponsible for this timeline change.
-
onPositionDiscontinuity
public void onPositionDiscontinuity(Player.PositionInfo oldPosition, Player.PositionInfo newPosition, @DiscontinuityReason @com.google.android.exoplayer2.Player.DiscontinuityReason int reason)
Description copied from interface:Player.EventListenerCalled when a position discontinuity occurs.A position discontinuity occurs when the playing period changes, the playback position jumps within the period currently being played, or when the playing period has been skipped or removed.
Player.EventListener.onEvents(Player, Events)will also be called to report this event along with other events that happen in the sameLoopermessage queue iteration.- Specified by:
onPositionDiscontinuityin interfacePlayer.EventListener- Specified by:
onPositionDiscontinuityin interfacePlayer.Listener- Parameters:
oldPosition- The position before the discontinuity.newPosition- The position after the discontinuity.reason- ThePlayer.DiscontinuityReasonresponsible for the discontinuity.
-
onShuffleModeEnabledChanged
public void onShuffleModeEnabledChanged(boolean shuffleModeEnabled)
Description copied from interface:Player.EventListenerCalled when the value ofPlayer.getShuffleModeEnabled()changes.Player.EventListener.onEvents(Player, Events)will also be called to report this event along with other events that happen in the sameLoopermessage queue iteration.- Specified by:
onShuffleModeEnabledChangedin interfacePlayer.EventListener- Specified by:
onShuffleModeEnabledChangedin interfacePlayer.Listener- Parameters:
shuffleModeEnabled- Whether shuffling ofmedia itemsis enabled.
-
onRepeatModeChanged
public void onRepeatModeChanged(@RepeatMode @com.google.android.exoplayer2.Player.RepeatMode int repeatMode)
Description copied from interface:Player.EventListenerCalled when the value ofPlayer.getRepeatMode()changes.Player.EventListener.onEvents(Player, Events)will also be called to report this event along with other events that happen in the sameLoopermessage queue iteration.- Specified by:
onRepeatModeChangedin interfacePlayer.EventListener- Specified by:
onRepeatModeChangedin interfacePlayer.Listener- Parameters:
repeatMode- ThePlayer.RepeatModeused for playback.
-
-