docs/doc/reference/com/google/android/exoplayer2/source/MediaSource.html
Package com.google.android.exoplayer2.source
AdsMediaSource, BaseMediaSource, ClippingMediaSource, CompositeMediaSource, ConcatenatingMediaSource, ConcatenatingMediaSource2, DashMediaSource, FakeAdaptiveMediaSource, FakeMediaSource, FilteringMediaSource, HlsMediaSource, ImaServerSideAdInsertionMediaSource, LoopingMediaSource, MaskingMediaSource, MergingMediaSource, ProgressiveMediaSource, RtspMediaSource, ServerSideAdInsertionMediaSource, SilenceMediaSource, SingleSampleMediaSource, SsMediaSource, WrappingMediaSource[@Deprecated](https://developer.android.com/reference/java/lang/Deprecated.html "class or interface in java.lang")public interfaceMediaSource
Deprecated. com.google.android.exoplayer2 is deprecated. Please migrate to androidx.media3 (which contains the same ExoPlayer code). See the migration guide for more details, including a script to help with the migration.
Defines and provides media to be played by an ExoPlayer. A MediaSource has two main responsibilities:
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 MediaSource.MediaSourceCaller.onSourceInfoRefreshed(com.google.android.exoplayer2.source.MediaSource, com.google.android.exoplayer2.Timeline) on the MediaSource.MediaSourceCallers passed to prepareSource(MediaSourceCaller, TransferListener, PlayerId).MediaPeriod instances for the periods in its timeline. MediaPeriods are obtained by calling createPeriod(MediaPeriodId, Allocator, long), 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.
Nested Classes | Modifier and Type | Interface | Description |
| --- | --- | --- |
| static interface | MediaSource.Factory |
Deprecated.
Factory for creating MediaSources from MediaItems.
|
| static class | MediaSource.MediaPeriodId |
Deprecated.
Identifier for a MediaPeriod.
|
| static interface | MediaSource.MediaSourceCaller |
Deprecated.
A caller of media sources, which will be notified of source events. |
All Methods Instance Methods Abstract Methods Default Methods Deprecated Methods | Modifier and Type | Method | Description |
| --- | --- | --- |
| void | addDrmEventListener(Handler handler, DrmSessionEventListener eventListener) |
Deprecated.
Adds a DrmSessionEventListener to the list of listeners which are notified of DRM events for this media source.
|
| void | addEventListener(Handler handler, MediaSourceEventListener eventListener) |
Deprecated.
Adds a MediaSourceEventListener to the list of listeners which are notified of media source events.
|
| MediaPeriod | createPeriod(MediaSource.MediaPeriodId id, Allocator allocator, long startPositionUs) |
Deprecated.
Returns a new MediaPeriod identified by periodId.
|
| void | disable(MediaSource.MediaSourceCaller caller) |
Deprecated.
Disables the source for the creation of MediaPeriods.
|
| void | enable(MediaSource.MediaSourceCaller caller) |
Deprecated.
Enables the source for the creation of MediaPeriods.
|
| default Timeline | getInitialTimeline() |
Deprecated.
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.
|
| MediaItem | getMediaItem() |
Deprecated.
Returns the MediaItem whose media is provided by the source.
|
| default boolean | isSingleWindow() |
Deprecated.
Returns true if the media source is guaranteed to never have zero or more than one window.
|
| void | maybeThrowSourceInfoRefreshError() |
Deprecated.
Throws any pending error encountered while loading or refreshing source information.
|
| default void | prepareSource(MediaSource.MediaSourceCaller caller, TransferListener mediaTransferListener) |
Deprecated.
Implement prepareSource(MediaSourceCaller, TransferListener, PlayerId) instead.
|
| void | prepareSource(MediaSource.MediaSourceCaller caller, TransferListener mediaTransferListener, PlayerId playerId) |
Deprecated.
Registers a MediaSource.MediaSourceCaller.
|
| void | releasePeriod(MediaPeriod mediaPeriod) |
Deprecated.
Releases the period.
|
| void | releaseSource(MediaSource.MediaSourceCaller caller) |
Deprecated.
Unregisters a caller, and disables and releases the source if no longer required.
|
| void | removeDrmEventListener(DrmSessionEventListener eventListener) |
Deprecated.
Removes a DrmSessionEventListener from the list of listeners which are notified of DRM events for this media source.
|
| void | removeEventListener(MediaSourceEventListener eventListener) |
Deprecated.
Removes a MediaSourceEventListener from the list of listeners which are notified of media source events.
|
-
void addEventListener([Handler](https://developer.android.com/reference/android/os/Handler.html "class or interface in android.os")handler,[MediaSourceEventListener](MediaSourceEventListener.html "interface in com.google.android.exoplayer2.source")eventListener)
Deprecated.
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 - A handler on the which listener events will be posted.eventListener - The listener to be added.
-
void removeEventListener([MediaSourceEventListener](MediaSourceEventListener.html "interface in com.google.android.exoplayer2.source")eventListener)
Deprecated.
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:eventListener - The listener to be removed.
-
void addDrmEventListener([Handler](https://developer.android.com/reference/android/os/Handler.html "class or interface in android.os")handler,[DrmSessionEventListener](../drm/DrmSessionEventListener.html "interface in com.google.android.exoplayer2.drm")eventListener)
Deprecated.
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 - A handler on the which listener events will be posted.eventListener - The listener to be added.
-
void removeDrmEventListener([DrmSessionEventListener](../drm/DrmSessionEventListener.html "interface in com.google.android.exoplayer2.drm")eventListener)
Deprecated.
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:eventListener - The listener to be removed.
-
@Nullable
default[Timeline](../Timeline.html "class in com.google.android.exoplayer2")getInitialTimeline()
Deprecated.
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.
-
default boolean isSingleWindow()
Deprecated.
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:true if the source has exactly one window.
-
[MediaItem](../MediaItem.html "class in com.google.android.exoplayer2")getMediaItem()
Deprecated.
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.
-
[@Deprecated](https://developer.android.com/reference/java/lang/Deprecated.html "class or interface in java.lang")default void prepareSource([MediaSource.MediaSourceCaller](MediaSource.MediaSourceCaller.html "interface in com.google.android.exoplayer2.source")caller,
@Nullable[TransferListener](../upstream/TransferListener.html "interface in com.google.android.exoplayer2.upstream")mediaTransferListener)
Deprecated.
Implement prepareSource(MediaSourceCaller, TransferListener, PlayerId) instead.
-
void prepareSource([MediaSource.MediaSourceCaller](MediaSource.MediaSourceCaller.html "interface in com.google.android.exoplayer2.source")caller,
@Nullable[TransferListener](../upstream/TransferListener.html "interface in com.google.android.exoplayer2.upstream")mediaTransferListener,[PlayerId](../analytics/PlayerId.html "class in com.google.android.exoplayer2.analytics")playerId)
Deprecated.
Registers a MediaSource.MediaSourceCaller. Starts source preparation if needed and enables the source for the creation of MediaPerods.
Should not be called directly from application code.
MediaSource.MediaSourceCaller.onSourceInfoRefreshed(MediaSource, Timeline) will be called once the source has a Timeline.
For each call to this method, a call to releaseSource(MediaSourceCaller) 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:caller - The MediaSource.MediaSourceCaller to be registered.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 - The PlayerId of the player using this media source.
-
void maybeThrowSourceInfoRefreshError()
throws[IOException](https://developer.android.com/reference/java/io/IOException.html "class or interface in java.io")
Deprecated.
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(MediaSourceCaller, TransferListener, PlayerId).
Throws:IOException
-
void enable([MediaSource.MediaSourceCaller](MediaSource.MediaSourceCaller.html "interface in com.google.android.exoplayer2.source")caller)
Deprecated.
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(MediaSourceCaller, TransferListener, PlayerId).
Parameters:caller - The MediaSource.MediaSourceCaller enabling the source.
-
[MediaPeriod](MediaPeriod.html "interface in com.google.android.exoplayer2.source")createPeriod([MediaSource.MediaPeriodId](MediaSource.MediaPeriodId.html "class in com.google.android.exoplayer2.source")id,[Allocator](../upstream/Allocator.html "interface in com.google.android.exoplayer2.upstream")allocator,
long startPositionUs)
Deprecated.
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: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.
-
void releasePeriod([MediaPeriod](MediaPeriod.html "interface in com.google.android.exoplayer2.source")mediaPeriod)
Deprecated.
Releases the period.
Should not be called directly from application code.
This method must be called on the playback thread.
Parameters:mediaPeriod - The period to release.
-
void disable([MediaSource.MediaSourceCaller](MediaSource.MediaSourceCaller.html "interface in com.google.android.exoplayer2.source")caller)
Deprecated.
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(MediaPeriodId, Allocator, long) have been released by releasePeriod(MediaPeriod).
Parameters:caller - The MediaSource.MediaSourceCaller disabling the source.
-
void releaseSource([MediaSource.MediaSourceCaller](MediaSource.MediaSourceCaller.html "interface in com.google.android.exoplayer2.source")caller)
Deprecated.
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(MediaPeriod).
Parameters:caller - The MediaSource.MediaSourceCaller to be unregistered.