Interface AdLoader

All Known Implementing Classes:
HlsInterstitialAdLoader, ServerAdLoader

public interface AdLoader
The interface to provide SDK with the Ad properties The implementations are used by ClientAdProvider.createAdLoader(AdRequest)
Since:
4.2.0
  • Method Details

    • getCurrentAd

      @Nullable Ad getCurrentAd()
      Get the currently loaded and prepared for playing Ad
      Returns:
      The current Ad or null
    • getAdContainer

      @Nullable ViewGroup getAdContainer()
      Returns ad UI container
      Returns:
      ad UI container
    • setPlayerController

      void setPlayerController(@Nullable PlayerController playerController)
      Set the active PlayerController instance or null if there is no active PlayerController or it is about to be released
      Parameters:
      playerController - The PlayerController instance or null
    • release

      void release()
      Release any unused resources
    • requiresMediaSourceHook

      default boolean requiresMediaSourceHook()
      Indicates whether this loader needs to create its own media source (client-side ads) or whether it operates on the content media source prepared by the player (server-side ads).
      Returns:
      true if a media-source hook is required, false otherwise.
    • createMediaSource

      void createMediaSource(@NonNull Object adsId, @NonNull PlayerConfig playerConfig, @NonNull PlayerPlugin playerPlugin, @NonNull AdLoader.Listener listener)
      Asynchronously create MediaSource to play
      Parameters:
      adsId - The unique to this PlayerConfig's ads id
      playerConfig - The PlayerConfig
      playerPlugin - The PlayerPlugin
      listener - The listener callback to provide the created MediaSource
    • onSetPosition

      long onSetPosition(long newPositionUs)
      Called just before the PlayerController start seeking or setting a new position. Can be used when the new position has to be adjusted by the AdLoader implementation 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

      void onPlayerStateChanged(boolean playWhenReady, int playbackState)
      Called when the player state is changed
      Parameters:
      playWhenReady - Play when ready flag
      playbackState - Playback state
    • onPositionDiscontinuity

      void onPositionDiscontinuity(@DiscontinuityReason @com.google.android.exoplayer2.Player.DiscontinuityReason int reason)
      Called when the playback discontinuity occurs
      Parameters:
      reason - The reason for discontinuity
    • scheduleAd

      void scheduleAd(@NonNull AdRequest adRequest)
      Start loading an AdRequest. This may be also called during playback. Support for on-request ad scheduling depends on the AdLoader implementation.
      Parameters:
      adRequest - The ad request to perform.
    • getAdApi

      @Nullable AdApi getAdApi()
      Get an AdApi to issue custom operations to the ad provider
      Returns:
      the AdApi
    • getAdPositionMs

      long getAdPositionMs(@NonNull Ad ad, @NonNull com.google.android.exoplayer2.ExoPlayer player)
      Returns the position of the provided Ad in milliseconds relative to the window that is currently playing (i.e. the media period exposed by Player.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.
    • onContentMediaSource

      default void onContentMediaSource(@NonNull PlayerConfig playerConfig, @NonNull com.google.android.exoplayer2.source.MediaSource mediaSource)
      Notifies the loader of a content MediaSource that 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 one PlayerConfig can 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; onContentMediaSourceReleased(PlayerConfig, MediaSource) reports the retirement of each one.
      Parameters:
      playerConfig - The PlayerConfig associated with the media source.
      mediaSource - The content media source that will be prepared.
    • onContentMediaSourceReleased

      default void onContentMediaSourceReleased(@NonNull PlayerConfig playerConfig, @NonNull com.google.android.exoplayer2.source.MediaSource mediaSource)
      Notifies the loader that a content MediaSource previously reported through 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 through 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:
      
       if (sources.get(playerConfig) == mediaSource) {
           sources.remove(playerConfig);
       }
       
      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 through setPlayerController(PlayerController) and release(), and any remaining per-configuration state must be dropped there.
      Parameters:
      playerConfig - The PlayerConfig the media source was built for. The same instance that was passed to onContentMediaSource(PlayerConfig, MediaSource).
      mediaSource - The content media source that has been retired.