public class SimpleExoPlayer extends java.lang.Object implements ExoPlayer
ExoPlayer implementation that uses default Renderer components. Instances can
be obtained from ExoPlayerFactory.| Modifier and Type | Class and Description |
|---|---|
static interface |
SimpleExoPlayer.VideoListener
A listener for video rendering information from a
SimpleExoPlayer. |
ExoPlayer.EventListener, ExoPlayer.ExoPlayerComponent, ExoPlayer.ExoPlayerMessage| Modifier and Type | Field and Description |
|---|---|
protected Renderer[] |
renderers |
STATE_BUFFERING, STATE_ENDED, STATE_IDLE, STATE_READY| Modifier | Constructor and Description |
|---|---|
protected |
SimpleExoPlayer(RenderersFactory renderersFactory,
TrackSelector trackSelector,
LoadControl loadControl) |
| Modifier and Type | Method and Description |
|---|---|
void |
addListener(ExoPlayer.EventListener listener)
Register a listener to receive events from the player.
|
void |
blockingSendMessages(ExoPlayer.ExoPlayerMessage... messages)
Variant of
ExoPlayer.sendMessages(ExoPlayerMessage...) that blocks until after the messages have
been delivered. |
void |
clearMetadataOutput(MetadataRenderer.Output output)
Clears the output receiving metadata events if it matches the one passed.
|
void |
clearTextOutput(TextRenderer.Output output)
Clears the output receiving text events if it matches the one passed.
|
void |
clearVideoListener(SimpleExoPlayer.VideoListener listener)
Clears the listener receiving video events if it matches the one passed.
|
void |
clearVideoSurface()
Clears any
Surface, SurfaceHolder, SurfaceView or TextureView
currently set on the player. |
void |
clearVideoSurface(android.view.Surface surface)
Clears the
Surface onto which video is being rendered if it matches the one passed. |
void |
clearVideoSurfaceHolder(android.view.SurfaceHolder surfaceHolder)
Clears the
SurfaceHolder that holds the Surface onto which video is being
rendered if it matches the one passed. |
void |
clearVideoSurfaceView(android.view.SurfaceView surfaceView)
Clears the
SurfaceView onto which video is being rendered if it matches the one passed. |
void |
clearVideoTextureView(android.view.TextureView textureView)
Clears the
TextureView onto which video is being rendered if it matches the one passed. |
DecoderCounters |
getAudioDecoderCounters()
Returns
DecoderCounters for audio, or null if no audio is being played. |
Format |
getAudioFormat()
Returns the audio format currently being played, or null if no audio is being played.
|
int |
getAudioSessionId()
Returns the audio session identifier, or
C.AUDIO_SESSION_ID_UNSET if not set. |
int |
getAudioStreamType()
Returns the stream type for audio playback.
|
int |
getBufferedPercentage()
Returns an estimate of the percentage in the current window up to which data is buffered, or 0
if no estimate is available.
|
long |
getBufferedPosition()
Returns an estimate of the position in the current window up to which data is buffered, in
milliseconds.
|
java.lang.Object |
getCurrentManifest()
Returns the current manifest.
|
int |
getCurrentPeriodIndex()
Returns the index of the period currently being played.
|
long |
getCurrentPosition()
Returns the playback position in the current window, in milliseconds.
|
Timeline |
getCurrentTimeline()
Returns the current
Timeline. |
TrackGroupArray |
getCurrentTrackGroups()
Returns the available track groups.
|
TrackSelectionArray |
getCurrentTrackSelections()
Returns the current track selections for each renderer.
|
int |
getCurrentWindowIndex()
Returns the index of the window currently being played.
|
long |
getDuration()
Returns the duration of the current window in milliseconds, or
C.TIME_UNSET if the
duration is not known. |
PlaybackParameters |
getPlaybackParameters()
Returns the currently active playback parameters.
|
int |
getPlaybackState()
Returns the current state of the player.
|
boolean |
getPlayWhenReady()
Whether playback will proceed when
ExoPlayer.getPlaybackState() == ExoPlayer.STATE_READY. |
int |
getRendererCount()
Returns the number of renderers.
|
int |
getRendererType(int index)
Returns the track type that the renderer at a given index handles.
|
DecoderCounters |
getVideoDecoderCounters()
Returns
DecoderCounters for video, or null if no video is being played. |
Format |
getVideoFormat()
Returns the video format currently being played, or null if no video is being played.
|
int |
getVideoScalingMode()
Returns the video scaling mode.
|
float |
getVolume()
Returns the audio volume, with 0 being silence and 1 being unity gain.
|
boolean |
isCurrentWindowDynamic()
Returns whether the current window is dynamic, or
false if the Timeline is
empty. |
boolean |
isCurrentWindowSeekable()
Returns whether the current window is seekable, or
false if the Timeline is
empty. |
boolean |
isLoading()
Whether the player is currently loading the source.
|
void |
prepare(MediaSource mediaSource)
Prepares the player to play the provided
MediaSource. |
void |
prepare(MediaSource mediaSource,
boolean resetPosition,
boolean resetState)
Prepares the player to play the provided
MediaSource, optionally resetting the playback
position the default position in the first Timeline.Window. |
void |
release()
Releases the player.
|
void |
removeListener(ExoPlayer.EventListener listener)
Unregister a listener.
|
void |
seekTo(int windowIndex,
long positionMs)
Seeks to a position specified in milliseconds in the specified window.
|
void |
seekTo(long positionMs)
Seeks to a position specified in milliseconds in the current window.
|
void |
seekToDefaultPosition()
Seeks to the default position associated with the current window.
|
void |
seekToDefaultPosition(int windowIndex)
Seeks to the default position associated with the specified window.
|
void |
sendMessages(ExoPlayer.ExoPlayerMessage... messages)
Sends messages to their target components.
|
void |
setAudioDebugListener(AudioRendererEventListener listener)
Sets a listener to receive debug events from the audio renderer.
|
void |
setAudioStreamType(int audioStreamType)
Sets the stream type for audio playback (see
C.StreamType and
AudioTrack.AudioTrack(int, int, int, int, int, int)). |
void |
setMetadataOutput(MetadataRenderer.Output output)
Sets a listener to receive metadata events.
|
void |
setPlaybackParameters(PlaybackParameters playbackParameters)
Attempts to set the playback parameters.
|
void |
setPlaybackParams(android.media.PlaybackParams params)
Deprecated.
|
void |
setPlayWhenReady(boolean playWhenReady)
Sets whether playback should proceed when
ExoPlayer.getPlaybackState() == ExoPlayer.STATE_READY. |
void |
setTextOutput(TextRenderer.Output output)
Sets an output to receive text events.
|
void |
setVideoDebugListener(VideoRendererEventListener listener)
Sets a listener to receive debug events from the video renderer.
|
void |
setVideoListener(SimpleExoPlayer.VideoListener listener)
Sets a listener to receive video events.
|
void |
setVideoScalingMode(int videoScalingMode)
Sets the video scaling mode.
|
void |
setVideoSurface(android.view.Surface surface)
Sets the
Surface onto which video will be rendered. |
void |
setVideoSurfaceHolder(android.view.SurfaceHolder surfaceHolder)
Sets the
SurfaceHolder that holds the Surface onto which video will be
rendered. |
void |
setVideoSurfaceView(android.view.SurfaceView surfaceView)
Sets the
SurfaceView onto which video will be rendered. |
void |
setVideoTextureView(android.view.TextureView textureView)
Sets the
TextureView onto which video will be rendered. |
void |
setVolume(float audioVolume)
Sets the audio volume, with 0 being silence and 1 being unity gain.
|
void |
stop()
Stops playback.
|
protected final Renderer[] renderers
protected SimpleExoPlayer(RenderersFactory renderersFactory, TrackSelector trackSelector, LoadControl loadControl)
public void setVideoScalingMode(int videoScalingMode)
Note that the scaling mode only applies if a MediaCodec-based video Renderer is
enabled and if the output surface is owned by a SurfaceView.
videoScalingMode - The video scaling mode.public int getVideoScalingMode()
public void clearVideoSurface()
Surface, SurfaceHolder, SurfaceView or TextureView
currently set on the player.public void setVideoSurface(android.view.Surface surface)
Surface onto which video will be rendered. The caller is responsible for
tracking the lifecycle of the surface, and must clear the surface by calling
setVideoSurface(null) if the surface is destroyed.
If the surface is held by a SurfaceView, TextureView or SurfaceHolder
then it's recommended to use setVideoSurfaceView(SurfaceView),
setVideoTextureView(TextureView) or setVideoSurfaceHolder(SurfaceHolder)
rather than this method, since passing the holder allows the player to track the lifecycle of
the surface automatically.
surface - The Surface.public void clearVideoSurface(android.view.Surface surface)
Surface onto which video is being rendered if it matches the one passed.
Else does nothing.surface - The surface to clear.public void setVideoSurfaceHolder(android.view.SurfaceHolder surfaceHolder)
SurfaceHolder that holds the Surface onto which video will be
rendered. The player will track the lifecycle of the surface automatically.surfaceHolder - The surface holder.public void clearVideoSurfaceHolder(android.view.SurfaceHolder surfaceHolder)
SurfaceHolder that holds the Surface onto which video is being
rendered if it matches the one passed. Else does nothing.surfaceHolder - The surface holder to clear.public void setVideoSurfaceView(android.view.SurfaceView surfaceView)
SurfaceView onto which video will be rendered. The player will track the
lifecycle of the surface automatically.surfaceView - The surface view.public void clearVideoSurfaceView(android.view.SurfaceView surfaceView)
SurfaceView onto which video is being rendered if it matches the one passed.
Else does nothing.surfaceView - The texture view to clear.public void setVideoTextureView(android.view.TextureView textureView)
TextureView onto which video will be rendered. The player will track the
lifecycle of the surface automatically.textureView - The texture view.public void clearVideoTextureView(android.view.TextureView textureView)
TextureView onto which video is being rendered if it matches the one passed.
Else does nothing.textureView - The texture view to clear.public void setAudioStreamType(int audioStreamType)
C.StreamType and
AudioTrack.AudioTrack(int, int, int, int, int, int)). If the stream type
is not set, audio renderers use C.STREAM_TYPE_DEFAULT.
Note that when the stream type changes, the AudioTrack must be reinitialized, which can introduce a brief gap in audio output. Note also that tracks in the same audio session must share the same routing, so a new audio session id will be generated.
audioStreamType - The stream type for audio playback.public int getAudioStreamType()
public void setVolume(float audioVolume)
audioVolume - The audio volume.public float getVolume()
@Deprecated
public void setPlaybackParams(@Nullable
android.media.PlaybackParams params)
setPlaybackParameters(PlaybackParameters).PlaybackParams governing audio playback.params - The PlaybackParams, or null to clear any previously set parameters.public Format getVideoFormat()
public Format getAudioFormat()
public int getAudioSessionId()
C.AUDIO_SESSION_ID_UNSET if not set.public DecoderCounters getVideoDecoderCounters()
DecoderCounters for video, or null if no video is being played.public DecoderCounters getAudioDecoderCounters()
DecoderCounters for audio, or null if no audio is being played.public void setVideoListener(SimpleExoPlayer.VideoListener listener)
listener - The listener.public void clearVideoListener(SimpleExoPlayer.VideoListener listener)
listener - The listener to clear.public void setTextOutput(TextRenderer.Output output)
output - The output.public void clearTextOutput(TextRenderer.Output output)
output - The output to clear.public void setMetadataOutput(MetadataRenderer.Output output)
output - The output.public void clearMetadataOutput(MetadataRenderer.Output output)
output - The output to clear.public void setVideoDebugListener(VideoRendererEventListener listener)
listener - The listener.public void setAudioDebugListener(AudioRendererEventListener listener)
listener - The listener.public void addListener(ExoPlayer.EventListener listener)
ExoPlayerLooper, then the listener will be called on the main thread.addListener in interface ExoPlayerlistener - The listener to register.public void removeListener(ExoPlayer.EventListener listener)
ExoPlayerremoveListener in interface ExoPlayerlistener - The listener to unregister.public int getPlaybackState()
ExoPlayergetPlaybackState in interface ExoPlayerSTATE constants defined in this interface.public void prepare(MediaSource mediaSource)
ExoPlayerMediaSource. Equivalent to
prepare(mediaSource, true, true).public void prepare(MediaSource mediaSource, boolean resetPosition, boolean resetState)
ExoPlayerMediaSource, optionally resetting the playback
position the default position in the first Timeline.Window.prepare in interface ExoPlayermediaSource - The MediaSource to play.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 ExoPlayer.getCurrentWindowIndex() and ExoPlayer.getCurrentPosition().resetState - Whether the timeline, manifest, tracks and track selections should be reset.
Should be true unless the player is being prepared to play the same media as it was playing
previously (e.g. if playback failed and is being retried).public void setPlayWhenReady(boolean playWhenReady)
ExoPlayerExoPlayer.getPlaybackState() == ExoPlayer.STATE_READY.
If the player is already in the ready state then this method can be used to pause and resume playback.
setPlayWhenReady in interface ExoPlayerplayWhenReady - Whether playback should proceed when ready.public boolean getPlayWhenReady()
ExoPlayerExoPlayer.getPlaybackState() == ExoPlayer.STATE_READY.getPlayWhenReady in interface ExoPlayerpublic boolean isLoading()
ExoPlayerpublic void seekToDefaultPosition()
ExoPlayerExoPlayer.prepare(MediaSource). For live streams it will typically
be the live edge of the window. For other streams it will typically be the start of the window.seekToDefaultPosition in interface ExoPlayerpublic void seekToDefaultPosition(int windowIndex)
ExoPlayerExoPlayer.prepare(MediaSource). For live streams it will typically
be the live edge of the window. For other streams it will typically be the start of the window.seekToDefaultPosition in interface ExoPlayerwindowIndex - The index of the window whose associated default position should be seeked
to.public void seekTo(long positionMs)
ExoPlayerseekTo in interface ExoPlayerpositionMs - The seek position in the current window, or C.TIME_UNSET to seek to
the window's default position.public void seekTo(int windowIndex,
long positionMs)
ExoPlayerseekTo in interface ExoPlayerwindowIndex - The index of the window.positionMs - The seek position in the specified window, or C.TIME_UNSET to seek to
the window's default position.public void setPlaybackParameters(PlaybackParameters playbackParameters)
ExoPlayernull sets the parameters to the
default, PlaybackParameters.DEFAULT, which means there is no speed or pitch adjustment.
Playback parameters changes may cause the player to buffer.
ExoPlayer.EventListener.onPlaybackParametersChanged(PlaybackParameters) will be called whenever
the currently active playback parameters change. When that listener is called, the parameters
passed to it may not match playbackParameters. For example, the chosen speed or pitch
may be out of range, in which case they are constrained to a set of permitted values. If it is
not possible to change the playback parameters, the listener will not be invoked.
setPlaybackParameters in interface ExoPlayerplaybackParameters - The playback parameters, or null to use the defaults.public PlaybackParameters getPlaybackParameters()
ExoPlayergetPlaybackParameters in interface ExoPlayerExoPlayer.EventListener.onPlaybackParametersChanged(PlaybackParameters)public void stop()
ExoPlayersetPlayWhenReady(false) rather than this method if the intention
is to pause playback.
Calling this method will cause the playback state to transition to ExoPlayer.STATE_IDLE. The
player instance can still be used, and ExoPlayer.release() must still be called on the player if
it's no longer required.
Calling this method does not reset the playback position.
public void release()
ExoPlayerpublic void sendMessages(ExoPlayer.ExoPlayerMessage... messages)
ExoPlayerExoPlaybackException then it is propagated out of the player
as an error.sendMessages in interface ExoPlayermessages - The messages to be sent.public void blockingSendMessages(ExoPlayer.ExoPlayerMessage... messages)
ExoPlayerExoPlayer.sendMessages(ExoPlayerMessage...) that blocks until after the messages have
been delivered.blockingSendMessages in interface ExoPlayermessages - The messages to be sent.public int getRendererCount()
ExoPlayergetRendererCount in interface ExoPlayerpublic int getRendererType(int index)
ExoPlayergetRendererType in interface ExoPlayerindex - The index of the renderer.TRACK_TYPE_* constants defined in C.Renderer.getTrackType()public TrackGroupArray getCurrentTrackGroups()
ExoPlayergetCurrentTrackGroups in interface ExoPlayerpublic TrackSelectionArray getCurrentTrackSelections()
ExoPlayergetCurrentTrackSelections in interface ExoPlayerpublic Timeline getCurrentTimeline()
ExoPlayerTimeline. Never null, but may be empty.getCurrentTimeline in interface ExoPlayerpublic java.lang.Object getCurrentManifest()
ExoPlayerMediaSource passed to
ExoPlayer.prepare(com.google.android.exoplayer2.source.MediaSource). May be null.getCurrentManifest in interface ExoPlayerpublic int getCurrentPeriodIndex()
ExoPlayergetCurrentPeriodIndex in interface ExoPlayerpublic int getCurrentWindowIndex()
ExoPlayergetCurrentWindowIndex in interface ExoPlayerpublic long getDuration()
ExoPlayerC.TIME_UNSET if the
duration is not known.getDuration in interface ExoPlayerpublic long getCurrentPosition()
ExoPlayergetCurrentPosition in interface ExoPlayerpublic long getBufferedPosition()
ExoPlayergetBufferedPosition in interface ExoPlayerpublic int getBufferedPercentage()
ExoPlayergetBufferedPercentage in interface ExoPlayerpublic boolean isCurrentWindowDynamic()
ExoPlayerfalse if the Timeline is
empty.isCurrentWindowDynamic in interface ExoPlayerTimeline.Window.isDynamicpublic boolean isCurrentWindowSeekable()
ExoPlayerfalse if the Timeline is
empty.isCurrentWindowSeekable in interface ExoPlayerTimeline.Window.isSeekable