public interface MediaSource

Known direct subclasses
BaseMediaSource

Base MediaSource implementation to handle parallel reuse and to keep a list of s.

Known indirect subclasses
AdsMediaSource

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

ClippingMediaSource

MediaSource that wraps a source and clips its timeline based on specified start/end positions.

CompositeMediaSource

Composite MediaSource consisting of multiple child sources.

ConcatenatingMediaSource2

Concatenates multiple MediaSources, combining everything in one single .

ConcatenatingMediaSource

This class is deprecated.

Use playlist modification methods like addMediaItem instead.

DashMediaSource

A DASH MediaSource.

ExternallyLoadedMediaSource

A MediaSource for media loaded outside of the usual ExoPlayer loading mechanism.

FakeAdaptiveMediaSource

Fake MediaSource that provides a given timeline.

FakeMediaSource

Fake MediaSource that provides a given timeline.

FilteringMediaSource

A MediaSource that filters the available track types.

HlsMediaSource

An HLS MediaSource.

ImaServerSideAdInsertionMediaSource

MediaSource for IMA server side inserted ad streams.

LoopingMediaSource

This class is deprecated.

To loop a MediaSource indefinitely, use setRepeatMode instead of this class.

MaskingMediaSource

A MediaSource that masks the Timeline with a placeholder until the actual media structure is known.

MergingMediaSource

Merges multiple MediaSources.

PreloadMediaSource

Preloads a MediaSource and provides a MediaPeriod that has data loaded before playback.

ProgressiveMediaSource

Provides one period that loads data from a Uri and extracted using an Extractor.

RtspMediaSource

An Rtsp MediaSource

ServerSideAdInsertionMediaSource

A MediaSource for server-side inserted ad breaks.

SilenceMediaSource

Media source with a single period consisting of silent raw audio of a given duration.

SingleSampleMediaSource

This class is deprecated.

The only use for this class is subtitle playback, but it is only compatible with legacy subtitle decoding, which is not supported by default.

SsMediaSource

A SmoothStreaming MediaSource.

WrappingMediaSource

An abstract MediaSource wrapping a single child MediaSource.


Defines and provides media to be played by an ExoPlayer. A MediaSource has two main responsibilities:

  • To provide the player with a Timeline defining the structure of its media, and to provide a new timeline whenever the structure of the media changes. The MediaSource provides these timelines by calling onSourceInfoRefreshed on the MediaSourceCallers passed to prepareSource.
  • To provide MediaPeriod instances for the periods in its timeline. MediaPeriods are obtained by calling createPeriod, and provide a way for the player to load and read the media.

MediaSource methods should not be called from application code. Instead, the playback logic in ExoPlayer will trigger methods at the right time.

Instances can be re-used, but only for one ExoPlayer instance simultaneously.

MediaSource methods will be called on one of two threads, an application thread or a playback thread. Each method is documented with the thread it is called on.

Summary

Nested types

public interface MediaSource.Factory

Factory for creating MediaSources from MediaItems.

Identifier for a MediaPeriod.

A caller of media sources, which will be notified of source events.

Public methods

abstract void
@UnstableApi
addDrmEventListener(
    Handler handler,
    DrmSessionEventListener eventListener
)

Adds a DrmSessionEventListener to the list of listeners which are notified of DRM events for this media source.

abstract void
@UnstableApi
addEventListener(
    Handler handler,
    MediaSourceEventListener eventListener
)

Adds a MediaSourceEventListener to the list of listeners which are notified of media source events.

default boolean

Returns whether the MediaItem for this source can be updated with the provided item.

abstract MediaPeriod
@UnstableApi
createPeriod(
    MediaSource.MediaPeriodId id,
    Allocator allocator,
    long startPositionUs
)

Returns a new MediaPeriod identified by periodId.

abstract void

Disables the source for the creation of MediaPeriods.

abstract void

Enables the source for the creation of MediaPeriods.

default @Nullable Timeline

Returns the initial placeholder timeline that is returned immediately when the real timeline is not yet known, or null to let the player create an initial timeline.

abstract MediaItem

Returns the MediaItem whose media is provided by the source.

default boolean

Returns true if the media source is guaranteed to never have zero or more than one window.

abstract void

Throws any pending error encountered while loading or refreshing source information.

default void
@UnstableApi
prepareSource(
    MediaSource.MediaSourceCaller caller,
    @Nullable TransferListener mediaTransferListener
)

This method is deprecated.

Implement prepareSource instead.

abstract void
@UnstableApi
prepareSource(
    MediaSource.MediaSourceCaller caller,
    @Nullable TransferListener mediaTransferListener,
    PlayerId playerId
)

Registers a MediaSourceCaller.

abstract void

Releases the period.

abstract void

Unregisters a caller, and disables and releases the source if no longer required.

abstract void

Removes a DrmSessionEventListener from the list of listeners which are notified of DRM events for this media source.

abstract void

Removes a MediaSourceEventListener from the list of listeners which are notified of media source events.

default void

Updates the MediaItem for this source.

Public methods

addDrmEventListener

@UnstableApi
abstract void addDrmEventListener(
    Handler handler,
    DrmSessionEventListener eventListener
)

Adds a DrmSessionEventListener to the list of listeners which are notified of DRM events for this media source.

Should not be called directly from application code.

This method must be called on the playback thread.

Parameters
Handler handler

A handler on the which listener events will be posted.

DrmSessionEventListener eventListener

The listener to be added.

addEventListener

@UnstableApi
abstract void addEventListener(
    Handler handler,
    MediaSourceEventListener eventListener
)

Adds a MediaSourceEventListener to the list of listeners which are notified of media source events.

Should not be called directly from application code.

This method must be called on the playback thread.

Parameters
Handler handler

A handler on the which listener events will be posted.

MediaSourceEventListener eventListener

The listener to be added.

canUpdateMediaItem

@UnstableApi
default boolean canUpdateMediaItem(MediaItem mediaItem)

Returns whether the MediaItem for this source can be updated with the provided item.

Should not be called directly from application code.

This method must be called on the application thread.

Parameters
MediaItem mediaItem

The new MediaItem.

Returns
boolean

Whether the source can be updated using this item.

createPeriod

@UnstableApi
abstract MediaPeriod createPeriod(
    MediaSource.MediaPeriodId id,
    Allocator allocator,
    long startPositionUs
)

Returns a new MediaPeriod identified by periodId.

Should not be called directly from application code.

This method must be called on the playback thread and only if the source is enabled.

Parameters
MediaSource.MediaPeriodId id

The identifier of the period.

Allocator allocator

An Allocator from which to obtain media buffer allocations.

long startPositionUs

The expected start position, in microseconds.

Returns
MediaPeriod

A new MediaPeriod.

disable

@UnstableApi
abstract void disable(MediaSource.MediaSourceCaller caller)

Disables the source for the creation of MediaPeriods. The implementation should not hold onto limited resources used for the creation of media periods.

Should not be called directly from application code.

This method must be called on the playback thread and only after all MediaPeriods previously created by createPeriod have been released by releasePeriod.

Parameters
MediaSource.MediaSourceCaller caller

The MediaSourceCaller disabling the source.

enable

@UnstableApi
abstract void enable(MediaSource.MediaSourceCaller caller)

Enables the source for the creation of MediaPeriods.

Should not be called directly from application code.

This method must be called on the playback thread and only after prepareSource.

Parameters
MediaSource.MediaSourceCaller caller

The MediaSourceCaller enabling the source.

getInitialTimeline

@UnstableApi
default @Nullable Timeline getInitialTimeline()

Returns the initial placeholder timeline that is returned immediately when the real timeline is not yet known, or null to let the player create an initial timeline.

Should not be called directly from application code.

The initial timeline must use the same uids for windows and periods that the real timeline will use. It also must provide windows which are marked as dynamic to indicate that the window is expected to change when the real timeline arrives.

Any media source which has multiple windows should typically provide such an initial timeline to make sure the player reports the correct number of windows immediately.

This method must be called on the application thread.

getMediaItem

@UnstableApi
abstract MediaItem getMediaItem()

Returns the MediaItem whose media is provided by the source.

Should not be called directly from application code.

This method must be called on the application thread.

isSingleWindow

@UnstableApi
default boolean isSingleWindow()

Returns true if the media source is guaranteed to never have zero or more than one window.

Should not be called directly from application code.

The default implementation returns true.

This method must be called on the application thread.

Returns
boolean

true if the source has exactly one window.

maybeThrowSourceInfoRefreshError

@UnstableApi
abstract void maybeThrowSourceInfoRefreshError()

Throws any pending error encountered while loading or refreshing source information.

Should not be called directly from application code.

This method must be called on the playback thread and only after prepareSource.

prepareSource

@UnstableApi
default void prepareSource(
    MediaSource.MediaSourceCaller caller,
    @Nullable TransferListener mediaTransferListener
)

prepareSource

@UnstableApi
abstract void prepareSource(
    MediaSource.MediaSourceCaller caller,
    @Nullable TransferListener mediaTransferListener,
    PlayerId playerId
)

Registers a MediaSourceCaller. Starts source preparation if needed and enables the source for the creation of MediaPerods.

Should not be called directly from application code.

onSourceInfoRefreshed will be called once the source has a Timeline.

For each call to this method, a call to releaseSource is needed to remove the caller and to release the source if no longer required.

This method must be called on the playback thread.

Parameters
MediaSource.MediaSourceCaller caller

The MediaSourceCaller to be registered.

@Nullable TransferListener 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 be only informed of transfers related to the media loads and not of auxiliary loads for manifests and other data.

PlayerId playerId

The PlayerId of the player using this media source.

releasePeriod

@UnstableApi
abstract void releasePeriod(MediaPeriod mediaPeriod)

Releases the period.

Should not be called directly from application code.

This method must be called on the playback thread.

Parameters
MediaPeriod mediaPeriod

The period to release.

releaseSource

@UnstableApi
abstract void releaseSource(MediaSource.MediaSourceCaller caller)

Unregisters a caller, and disables and releases the source if no longer required.

Should not be called directly from application code.

This method must be called on the playback thread and only if all created MediaPeriods have been released by releasePeriod.

Parameters
MediaSource.MediaSourceCaller caller

The MediaSourceCaller to be unregistered.

removeDrmEventListener

@UnstableApi
abstract void removeDrmEventListener(DrmSessionEventListener eventListener)

Removes a DrmSessionEventListener from the list of listeners which are notified of DRM events for this media source.

Should not be called directly from application code.

This method must be called on the playback thread.

Parameters
DrmSessionEventListener eventListener

The listener to be removed.

removeEventListener

@UnstableApi
abstract void removeEventListener(MediaSourceEventListener eventListener)

Removes a MediaSourceEventListener from the list of listeners which are notified of media source events.

Should not be called directly from application code.

This method must be called on the playback thread.

Parameters
MediaSourceEventListener eventListener

The listener to be removed.

updateMediaItem

@UnstableApi
default void updateMediaItem(MediaItem mediaItem)

Updates the MediaItem for this source.

Should not be called directly from application code.

This method must be called on the playback thread and only if canUpdateMediaItem returns true for the new MediaItem.

Parameters
MediaItem mediaItem

The new MediaItem.