Package com.google.android.exoplayer2.source.ads

## Class AdsMediaSource

- java.lang.Object
- - com.google.android.exoplayer2.source.BaseMediaSource
  - - com.google.android.exoplayer2.source.CompositeMediaSource<MediaSource.MediaPeriodId>
    - - com.google.android.exoplayer2.source.ads.AdsMediaSource

- All Implemented Interfaces:
    `MediaSource`

      public final class AdsMediaSource
      extends CompositeMediaSource<MediaSource.MediaPeriodId>

  A `MediaSource` that inserts ads linearly into a provided content media source.

  The wrapped content media source must contain a single `Timeline.Period`.

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class | Description
    --- | --- | ---
    `static class` | `AdsMediaSource.AdLoadException` | Wrapper for exceptions that occur while loading ads, which are notified via `MediaSourceEventListener.onLoadError(int, MediaPeriodId, LoadEventInfo, MediaLoadData, IOException, boolean)`.

    - ### Nested classes/interfaces inherited from interface com.google.android.exoplayer2.source.MediaSource

      `MediaSource.MediaPeriodId, MediaSource.MediaSourceCaller`

  - ### Constructor Summary

    Constructors
    Constructor | Description
    --- | ---
    `AdsMediaSource​(MediaSource contentMediaSource, DataSpec adTagDataSpec, Object adsId, MediaSourceFactory adMediaSourceFactory, AdsLoader adsLoader, AdViewProvider adViewProvider)` | Constructs a new source that inserts ads linearly with the content specified by `contentMediaSource`.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method | Description
    --- | --- | ---
    `MediaPeriod` | `createPeriod​(MediaSource.MediaPeriodId id, Allocator allocator, long startPositionUs)` | Returns a new `MediaPeriod` identified by `periodId`.
    `MediaItem` | `getMediaItem()` | Returns the `MediaItem` whose media is provided by the source.
    `protected MediaSource.MediaPeriodId` | `getMediaPeriodIdForChildMediaPeriodId​(MediaSource.MediaPeriodId childId, MediaSource.MediaPeriodId mediaPeriodId)` | Returns the `MediaSource.MediaPeriodId` in the composite source corresponding to the specified `MediaSource.MediaPeriodId` in a child source.
    `protected void` | `onChildSourceInfoRefreshed​(MediaSource.MediaPeriodId mediaPeriodId, MediaSource mediaSource, Timeline timeline)` | Called when the source info of a child source has been refreshed.
    `protected void` | `prepareSourceInternal​(TransferListener mediaTransferListener)` | Starts source preparation and enables the source, see `BaseMediaSource.prepareSource(MediaSourceCaller, TransferListener)`.
    `void` | `releasePeriod​(MediaPeriod mediaPeriod)` | Releases the period.
    `protected void` | `releaseSourceInternal()` | Releases the source, see `BaseMediaSource.releaseSource(MediaSourceCaller)`.

    - ### Methods inherited from class com.google.android.exoplayer2.source.CompositeMediaSource

      `disableChildSource, disableInternal, enableChildSource, enableInternal, getMediaTimeForChildMediaTime, getWindowIndexForChildWindowIndex, maybeThrowSourceInfoRefreshError, prepareChildSource, releaseChildSource`

    - ### Methods inherited from class com.google.android.exoplayer2.source.BaseMediaSource

      `addDrmEventListener, addEventListener, createDrmEventDispatcher, createDrmEventDispatcher, createEventDispatcher, createEventDispatcher, createEventDispatcher, disable, enable, isEnabled, prepareSource, refreshSourceInfo, releaseSource, removeDrmEventListener, removeEventListener`

    - ### 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.source.MediaSource

      `getInitialTimeline, isSingleWindow`

- - ### Constructor Detail

    - #### AdsMediaSource

          public AdsMediaSource​(MediaSource contentMediaSource,
                                DataSpec adTagDataSpec,
                                Object adsId,
                                MediaSourceFactory adMediaSourceFactory,
                                AdsLoader adsLoader,
                                AdViewProvider adViewProvider)

      Constructs a new source that inserts ads linearly with the content specified by `contentMediaSource`.
      Parameters:
        `contentMediaSource` - The `MediaSource` providing the content to play.
        `adTagDataSpec` - The data specification of the ad tag to load.
        `adsId` - An opaque identifier for ad playback state associated with this instance. Ad loading and playback state is shared among all playlist items that have the same ads id (by `equality`), so it is important to pass the same identifiers when constructing playlist items each time the player returns to the foreground.
        `adMediaSourceFactory` - Factory for media sources used to load ad media.
        `adsLoader` - The loader for ads.
        `adViewProvider` - Provider of views for the ad UI.

  - ### Method Detail

    - #### getMediaItem

          public MediaItem getMediaItem()

      Description copied from interface: `MediaSource`
      Returns the `MediaItem` whose media is provided by the source.

    - #### prepareSourceInternal

          protected void prepareSourceInternal​(@Nullable
                                               TransferListener mediaTransferListener)

      Description copied from class: `BaseMediaSource`
      Starts source preparation and enables the source, see `BaseMediaSource.prepareSource(MediaSourceCaller, TransferListener)`. This method is called at most once until the next call to `BaseMediaSource.releaseSourceInternal()`.
      Overrides:
        `prepareSourceInternal` in class `CompositeMediaSource<MediaSource.MediaPeriodId>`
      Parameters:
        `mediaTransferListener` - The transfer listener which should be informed of any media data transfers. May be null if no listener is available. Note that this listener should usually be only informed of transfers related to the media loads and not of auxiliary loads for manifests and other data.

    - #### createPeriod

          public MediaPeriod createPeriod​(MediaSource.MediaPeriodId id,
                                          Allocator allocator,
                                          long startPositionUs)

      Description copied from interface: `MediaSource`
      Returns a new `MediaPeriod` identified by `periodId`.

      Should not be called directly from application code.

      Must only be called if the source is enabled.

      Parameters:
        `id` - The identifier of the period.
        `allocator` - An `Allocator` from which to obtain media buffer allocations.
        `startPositionUs` - The expected start position, in microseconds.
      Returns:
        A new `MediaPeriod`.

    - #### releasePeriod

          public void releasePeriod​(MediaPeriod mediaPeriod)

      Description copied from interface: `MediaSource`
      Releases the period.

      Should not be called directly from application code.

      Parameters:
        `mediaPeriod` - The period to release.

    - #### releaseSourceInternal

          protected void releaseSourceInternal()

      Description copied from class: `BaseMediaSource`
      Releases the source, see `BaseMediaSource.releaseSource(MediaSourceCaller)`. This method is called exactly once after each call to `BaseMediaSource.prepareSourceInternal(TransferListener)`.
      Overrides:
        `releaseSourceInternal` in class `CompositeMediaSource<MediaSource.MediaPeriodId>`

    - #### onChildSourceInfoRefreshed

          protected void onChildSourceInfoRefreshed​(MediaSource.MediaPeriodId mediaPeriodId,
                                                    MediaSource mediaSource,
                                                    Timeline timeline)

      Description copied from class: `CompositeMediaSource`
      Called when the source info of a child source has been refreshed.
      Specified by:
        `onChildSourceInfoRefreshed` in class `CompositeMediaSource<MediaSource.MediaPeriodId>`
      Parameters:
        `mediaPeriodId` - The unique id used to prepare the child source.
        `mediaSource` - The child source whose source info has been refreshed.
        `timeline` - The timeline of the child source.

    - #### getMediaPeriodIdForChildMediaPeriodId

          protected MediaSource.MediaPeriodId getMediaPeriodIdForChildMediaPeriodId​(MediaSource.MediaPeriodId childId,
                                                                                    MediaSource.MediaPeriodId mediaPeriodId)

      Description copied from class: `CompositeMediaSource`
      Returns the `MediaSource.MediaPeriodId` in the composite source corresponding to the specified `MediaSource.MediaPeriodId` in a child source. The default implementation does not change the media period id.
      Overrides:
        `getMediaPeriodIdForChildMediaPeriodId` in class `CompositeMediaSource<MediaSource.MediaPeriodId>`
      Parameters:
        `childId` - The unique id used to prepare the child source.
        `mediaPeriodId` - A `MediaSource.MediaPeriodId` of the child source.
      Returns:
        The corresponding `MediaSource.MediaPeriodId` in the composite source. Null if no corresponding media period id can be determined.