Player
SimpleExoPlayer
, StubExoPlayer
public interface ExoPlayer extends Player
MediaSource
s. Instances can be obtained from SimpleExoPlayer.Builder
or ExoPlayer.Builder
.
ExoPlayer is designed to make few assumptions about (and hence impose few restrictions on) the type of the media being played, how and where it is stored, and how it is rendered. Rather than implementing the loading and rendering of media directly, ExoPlayer implementations delegate this work to components that are injected when a player is created or when it's prepared for playback. Components common to all ExoPlayer implementations are:
MediaSource
that defines the media to be played, loads the media, and from
which the loaded media can be read. A MediaSource is injected via setMediaSource(MediaSource)
at the start of playback. The library modules provide default
implementations for progressive media files (ProgressiveMediaSource
), DASH
(DashMediaSource), SmoothStreaming (SsMediaSource) and HLS (HlsMediaSource), an
implementation for loading single media samples (SingleSampleMediaSource
) that's
most often used for side-loaded subtitle files, and implementations for building more
complex MediaSources from simpler ones (MergingMediaSource
, ConcatenatingMediaSource
, LoopingMediaSource
and ClippingMediaSource
).
Renderer
s that render individual components of the media. The library
provides default implementations for common media types (MediaCodecVideoRenderer
,
MediaCodecAudioRenderer
, TextRenderer
and MetadataRenderer
). A
Renderer consumes media from the MediaSource being played. Renderers are injected when the
player is created.
TrackSelector
that selects tracks provided by the MediaSource to be
consumed by each of the available Renderers. The library provides a default implementation
(DefaultTrackSelector
) suitable for most use cases. A TrackSelector is injected
when the player is created.
LoadControl
that controls when the MediaSource buffers more media, and how
much media is buffered. The library provides a default implementation (DefaultLoadControl
) suitable for most use cases. A LoadControl is injected when the player
is created.
An ExoPlayer can be built using the default components provided by the library, but may also be built using custom implementations if non-standard behaviors are required. For example a custom LoadControl could be injected to change the player's buffering strategy, or a custom Renderer could be injected to add support for a video codec not supported natively by Android.
The concept of injecting components that implement pieces of player functionality is present
throughout the library. The default component implementations listed above delegate work to
further injected components. This allows many sub-components to be individually replaced with
custom implementations. For example the default MediaSource implementations require one or more
DataSource
factories to be injected via their constructors. By providing a custom factory
it's possible to load data from a non-standard source, or through a different network stack.
The figure below shows ExoPlayer's threading model.
Player.getApplicationLooper()
.
Player.getApplicationLooper()
. Note that this means registered listeners are called on the same
thread which must be used to access the player.
Modifier and Type | Interface | Description |
---|---|---|
static class |
ExoPlayer.Builder |
A builder for
ExoPlayer instances. |
Player.AudioComponent, Player.DefaultEventListener, Player.DeviceComponent, Player.DiscontinuityReason, Player.EventFlags, Player.EventListener, Player.Events, Player.MediaItemTransitionReason, Player.MetadataComponent, Player.PlaybackSuppressionReason, Player.PlayWhenReadyChangeReason, Player.RepeatMode, Player.State, Player.TextComponent, Player.TimelineChangeReason, Player.VideoComponent
Modifier and Type | Field | Description |
---|---|---|
static long |
DEFAULT_RELEASE_TIMEOUT_MS |
The default timeout for calls to
Player.release() and setForegroundMode(boolean) , in
milliseconds. |
DISCONTINUITY_REASON_AD_INSERTION, DISCONTINUITY_REASON_INTERNAL, DISCONTINUITY_REASON_PERIOD_TRANSITION, DISCONTINUITY_REASON_SEEK, DISCONTINUITY_REASON_SEEK_ADJUSTMENT, EVENT_IS_LOADING_CHANGED, EVENT_IS_PLAYING_CHANGED, EVENT_MEDIA_ITEM_TRANSITION, EVENT_PLAY_WHEN_READY_CHANGED, EVENT_PLAYBACK_PARAMETERS_CHANGED, EVENT_PLAYBACK_STATE_CHANGED, EVENT_PLAYBACK_SUPPRESSION_REASON_CHANGED, EVENT_PLAYER_ERROR, EVENT_POSITION_DISCONTINUITY, EVENT_REPEAT_MODE_CHANGED, EVENT_SHUFFLE_MODE_ENABLED_CHANGED, EVENT_STATIC_METADATA_CHANGED, EVENT_TIMELINE_CHANGED, EVENT_TRACKS_CHANGED, MEDIA_ITEM_TRANSITION_REASON_AUTO, MEDIA_ITEM_TRANSITION_REASON_PLAYLIST_CHANGED, MEDIA_ITEM_TRANSITION_REASON_REPEAT, MEDIA_ITEM_TRANSITION_REASON_SEEK, PLAY_WHEN_READY_CHANGE_REASON_AUDIO_BECOMING_NOISY, PLAY_WHEN_READY_CHANGE_REASON_AUDIO_FOCUS_LOSS, PLAY_WHEN_READY_CHANGE_REASON_END_OF_MEDIA_ITEM, PLAY_WHEN_READY_CHANGE_REASON_REMOTE, PLAY_WHEN_READY_CHANGE_REASON_USER_REQUEST, PLAYBACK_SUPPRESSION_REASON_NONE, PLAYBACK_SUPPRESSION_REASON_TRANSIENT_AUDIO_FOCUS_LOSS, REPEAT_MODE_ALL, REPEAT_MODE_OFF, REPEAT_MODE_ONE, STATE_BUFFERING, STATE_ENDED, STATE_IDLE, STATE_READY, TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED, TIMELINE_CHANGE_REASON_SOURCE_UPDATE
Modifier and Type | Method | Description |
---|---|---|
void |
addMediaSource(int index,
MediaSource mediaSource) |
Adds a media source at the given index of the playlist.
|
void |
addMediaSource(MediaSource mediaSource) |
Adds a media source to the end of the playlist.
|
void |
addMediaSources(int index,
List<MediaSource> mediaSources) |
Adds a list of media sources at the given index of the playlist.
|
void |
addMediaSources(List<MediaSource> mediaSources) |
Adds a list of media sources to the end of the playlist.
|
PlayerMessage |
createMessage(PlayerMessage.Target target) |
Creates a message that can be sent to a
PlayerMessage.Target . |
boolean |
experimentalIsSleepingForOffload() |
Returns whether the player has paused its main loop to save power in offload scheduling mode.
|
void |
experimentalSetOffloadSchedulingEnabled(boolean offloadSchedulingEnabled) |
Sets whether audio offload scheduling is enabled.
|
Clock |
getClock() |
Returns the
Clock used for playback. |
boolean |
getPauseAtEndOfMediaItems() |
Returns whether the player pauses playback at the end of each media item.
|
Looper |
getPlaybackLooper() |
Returns the
Looper associated with the playback thread. |
SeekParameters |
getSeekParameters() |
Returns the currently active
SeekParameters of the player. |
TrackSelector |
getTrackSelector() |
Returns the track selector that this player uses, or null if track selection is not supported.
|
void |
prepare(MediaSource mediaSource) |
Deprecated.
Use
setMediaSource(MediaSource) and Player.prepare() instead. |
void |
prepare(MediaSource mediaSource,
boolean resetPosition,
boolean resetState) |
Deprecated.
Use
setMediaSource(MediaSource, boolean) and Player.prepare() instead. |
void |
retry() |
Deprecated.
Use
Player.prepare() instead. |
void |
setForegroundMode(boolean foregroundMode) |
Sets whether the player is allowed to keep holding limited resources such as video decoders,
even when in the idle state.
|
void |
setMediaSource(MediaSource mediaSource) |
Clears the playlist, adds the specified
MediaSource and resets the position to the
default position. |
void |
setMediaSource(MediaSource mediaSource,
boolean resetPosition) |
Clears the playlist and adds the specified
MediaSource . |
void |
setMediaSource(MediaSource mediaSource,
long startPositionMs) |
Clears the playlist and adds the specified
MediaSource . |
void |
setMediaSources(List<MediaSource> mediaSources) |
Clears the playlist, adds the specified
MediaSources and resets the
position to the default position. |
void |
setMediaSources(List<MediaSource> mediaSources,
boolean resetPosition) |
Clears the playlist and adds the specified
MediaSources . |
void |
setMediaSources(List<MediaSource> mediaSources,
int startWindowIndex,
long startPositionMs) |
Clears the playlist and adds the specified
MediaSources . |
void |
setPauseAtEndOfMediaItems(boolean pauseAtEndOfMediaItems) |
Sets whether to pause playback at the end of each media item.
|
void |
setSeekParameters(SeekParameters seekParameters) |
Sets the parameters that control how seek operations are performed.
|
void |
setShuffleOrder(ShuffleOrder shuffleOrder) |
Sets the shuffle order.
|
addListener, addMediaItem, addMediaItem, addMediaItems, addMediaItems, clearMediaItems, getApplicationLooper, getAudioComponent, getBufferedPercentage, getBufferedPosition, getContentBufferedPosition, getContentDuration, getContentPosition, getCurrentAdGroupIndex, getCurrentAdIndexInAdGroup, getCurrentLiveOffset, getCurrentManifest, getCurrentMediaItem, getCurrentPeriodIndex, getCurrentPosition, getCurrentStaticMetadata, getCurrentTag, getCurrentTimeline, getCurrentTrackGroups, getCurrentTrackSelections, getCurrentWindowIndex, getDeviceComponent, getDuration, getMediaItemAt, getMediaItemCount, getMetadataComponent, getNextWindowIndex, getPlaybackError, getPlaybackParameters, getPlaybackState, getPlaybackSuppressionReason, getPlayerError, getPlayWhenReady, getPreviousWindowIndex, getRendererCount, getRendererType, getRepeatMode, getShuffleModeEnabled, getTextComponent, getTotalBufferedDuration, getVideoComponent, hasNext, hasPrevious, isCurrentWindowDynamic, isCurrentWindowLive, isCurrentWindowSeekable, isLoading, isPlaying, isPlayingAd, moveMediaItem, moveMediaItems, next, pause, play, prepare, previous, release, removeListener, removeMediaItem, removeMediaItems, seekTo, seekTo, seekToDefaultPosition, seekToDefaultPosition, setMediaItem, setMediaItem, setMediaItem, setMediaItems, setMediaItems, setMediaItems, setPlaybackParameters, setPlayWhenReady, setRepeatMode, setShuffleModeEnabled, stop, stop
static final long DEFAULT_RELEASE_TIMEOUT_MS
Player.release()
and setForegroundMode(boolean)
, in
milliseconds.@Nullable TrackSelector getTrackSelector()
Looper getPlaybackLooper()
Looper
associated with the playback thread.@Deprecated void retry()
Player.prepare()
instead.@Deprecated void prepare(MediaSource mediaSource)
setMediaSource(MediaSource)
and Player.prepare()
instead.@Deprecated void prepare(MediaSource mediaSource, boolean resetPosition, boolean resetState)
setMediaSource(MediaSource, boolean)
and Player.prepare()
instead.void setMediaSources(List<MediaSource> mediaSources)
MediaSources
and resets the
position to the default position.mediaSources
- The new MediaSources
.void setMediaSources(List<MediaSource> mediaSources, boolean resetPosition)
MediaSources
.mediaSources
- The new MediaSources
.resetPosition
- Whether the playback position should be reset to the default position in
the first Timeline.Window
. If false, playback will start from the position defined
by Player.getCurrentWindowIndex()
and Player.getCurrentPosition()
.void setMediaSources(List<MediaSource> mediaSources, int startWindowIndex, long startPositionMs)
MediaSources
.mediaSources
- The new MediaSources
.startWindowIndex
- The window index to start playback from. If C.INDEX_UNSET
is
passed, the current position is not reset.startPositionMs
- The position in milliseconds to start playback from. If C.TIME_UNSET
is passed, the default position of the given window is used. In any case, if
startWindowIndex
is set to C.INDEX_UNSET
, this parameter is ignored and the
position is not reset at all.void setMediaSource(MediaSource mediaSource)
MediaSource
and resets the position to the
default position.mediaSource
- The new MediaSource
.void setMediaSource(MediaSource mediaSource, long startPositionMs)
MediaSource
.mediaSource
- The new MediaSource
.startPositionMs
- The position in milliseconds to start playback from.void setMediaSource(MediaSource mediaSource, boolean resetPosition)
MediaSource
.mediaSource
- The new MediaSource
.resetPosition
- Whether the playback position should be reset to the default position. If
false, playback will start from the position defined by Player.getCurrentWindowIndex()
and Player.getCurrentPosition()
.void addMediaSource(MediaSource mediaSource)
mediaSource
- The MediaSource
to add.void addMediaSource(int index, MediaSource mediaSource)
index
- The index at which to add the source.mediaSource
- The MediaSource
to add.void addMediaSources(List<MediaSource> mediaSources)
mediaSources
- The MediaSources
to add.void addMediaSources(int index, List<MediaSource> mediaSources)
index
- The index at which to add the media sources.mediaSources
- The MediaSources
to add.void setShuffleOrder(ShuffleOrder shuffleOrder)
shuffleOrder
- The shuffle order.PlayerMessage createMessage(PlayerMessage.Target target)
PlayerMessage.Target
. By default, the message
will be delivered immediately without blocking on the playback thread. The default PlayerMessage.getType()
is 0 and the default PlayerMessage.getPayload()
is null. If a
position is specified with PlayerMessage.setPosition(long)
, the message will be
delivered at this position in the current window defined by Player.getCurrentWindowIndex()
.
Alternatively, the message can be sent at a specific window using PlayerMessage.setPosition(int, long)
.void setSeekParameters(@Nullable SeekParameters seekParameters)
seekParameters
- The seek parameters, or null
to use the defaults.SeekParameters getSeekParameters()
SeekParameters
of the player.void setForegroundMode(boolean foregroundMode)
This mode should be used with caution, since holding limited resources may prevent other players of media components from acquiring them. It should only be enabled when both of the following conditions are true:
Player.stop()
is called to halt one playback, and
prepare(com.google.android.exoplayer2.source.MediaSource)
is called some time later to start a new one).
Note that foreground mode is not useful for switching between content without gaps
between the playbacks. For this use case Player.stop()
does not need to be called, and simply
calling prepare(com.google.android.exoplayer2.source.MediaSource)
for the new media will cause limited resources to be retained even if
foreground mode is not enabled.
If foreground mode is enabled, it's the application's responsibility to disable it when the conditions described above no longer hold.
foregroundMode
- Whether the player is allowed to keep limited resources even when in the
idle state.void setPauseAtEndOfMediaItems(boolean pauseAtEndOfMediaItems)
This means the player will pause at the end of each window in the current timeline
. Listeners will be informed by a call to Player.EventListener.onPlayWhenReadyChanged(boolean, int)
with the reason Player.PLAY_WHEN_READY_CHANGE_REASON_END_OF_MEDIA_ITEM
when this happens.
pauseAtEndOfMediaItems
- Whether to pause playback at the end of each media item.boolean getPauseAtEndOfMediaItems()
setPauseAtEndOfMediaItems(boolean)
void experimentalSetOffloadSchedulingEnabled(boolean offloadSchedulingEnabled)
Only use this scheduling mode if the player is not displaying anything to the user. For example when the application is in the background, or the screen is off. The player state (including position) is rarely updated (roughly between every 10 seconds and 1 minute).
While offload scheduling is enabled, player events may be delivered severely delayed and
apps should not interact with the player. When returning to the foreground, disable offload
scheduling and wait for Player.EventListener.onExperimentalOffloadSchedulingEnabledChanged(boolean)
to be called with
offloadSchedulingEnabled = false
before interacting with the player.
This mode should save significant power when the phone is playing offload audio with the screen off.
This mode only has an effect when playing an audio track in offload mode, which requires all the following:
DefaultRenderersFactory.setEnableAudioOffload(boolean)
or the equivalent option passed to DefaultAudioSink(AudioCapabilities,
DefaultAudioSink.AudioProcessorChain, boolean, boolean, boolean)
.
AudioSink
is playing with an offload AudioTrack
.
The state where ExoPlayer main loop has been paused to save power during offload playback
can be queried with experimentalIsSleepingForOffload()
.
This method is experimental, and will be renamed or removed in a future release.
offloadSchedulingEnabled
- Whether to enable offload scheduling.boolean experimentalIsSleepingForOffload()