public abstract class TrackRenderer extends Object implements ExoPlayer.ExoPlayerComponent
Internally, a renderer's lifecycle is managed by the owning ExoPlayer
. The player
will transition its renderers through various states as the overall playback state changes. The
valid state transitions are shown below, annotated with the methods that are invoked during each
transition.
Modifier and Type | Field and Description |
---|---|
static long |
END_OF_TRACK_US
Represents the time of the end of the track.
|
static long |
MATCH_LONGEST_US
Represents a time or duration that should match the duration of the longest track whose
duration is known.
|
protected static int |
STATE_ENABLED
The renderer is enabled.
|
protected static int |
STATE_PREPARED
The renderer has completed necessary preparation.
|
protected static int |
STATE_RELEASED
The renderer has been released and should not be used.
|
protected static int |
STATE_STARTED
The renderer is started.
|
protected static int |
STATE_UNPREPARED
The renderer has not yet been prepared.
|
static long |
UNKNOWN_TIME_US
Represents an unknown time or duration.
|
Constructor and Description |
---|
TrackRenderer() |
Modifier and Type | Method and Description |
---|---|
protected abstract boolean |
doPrepare(long positionUs)
Invoked to make progress when the renderer is in the
STATE_UNPREPARED state. |
protected abstract void |
doSomeWork(long positionUs,
long elapsedRealtimeUs)
Invoked to make progress when the renderer is in the
STATE_ENABLED or
STATE_STARTED states. |
protected abstract long |
getBufferedPositionUs()
Returns an estimate of the absolute position in microseconds up to which data is buffered.
|
protected abstract long |
getDurationUs()
Returns the duration of the media being rendered.
|
protected abstract MediaFormat |
getFormat(int track)
Returns the format of the specified track.
|
protected MediaClock |
getMediaClock()
If the renderer advances its own playback position then this method returns a corresponding
MediaClock . |
protected int |
getState()
Returns the current state of the renderer.
|
protected abstract int |
getTrackCount()
Returns the number of tracks exposed by the renderer.
|
void |
handleMessage(int what,
Object object)
Handles a message delivered to the component.
|
protected abstract boolean |
isEnded()
Whether the renderer is ready for the
ExoPlayer instance to transition to
ExoPlayer.STATE_ENDED . |
protected abstract boolean |
isReady()
Whether the renderer is able to immediately render media from the current position.
|
protected abstract void |
maybeThrowError()
Throws an error that's preventing the renderer from making progress or buffering more data at
this point in time.
|
protected void |
onDisabled()
Called when the renderer is disabled.
|
protected void |
onEnabled(int track,
long positionUs,
boolean joining)
Called when the renderer is enabled.
|
protected void |
onReleased()
Called when the renderer is released.
|
protected void |
onStarted()
Called when the renderer is started.
|
protected void |
onStopped()
Called when the renderer is stopped.
|
protected abstract void |
seekTo(long positionUs)
Seeks to a specified time in the track.
|
public static final long UNKNOWN_TIME_US
C.UNKNOWN_TIME_US
.public static final long MATCH_LONGEST_US
C.MATCH_LONGEST_US
.public static final long END_OF_TRACK_US
protected static final int STATE_RELEASED
protected static final int STATE_UNPREPARED
protected static final int STATE_PREPARED
The renderer should not hold scarce or expensive system resources (e.g. media decoders) and should not be actively buffering media data when in this state.
protected static final int STATE_ENABLED
protected static final int STATE_STARTED
doSomeWork(long, long)
should cause the media to be
rendered.protected MediaClock getMediaClock()
MediaClock
. If provided, the player will use the returned MediaClock
as its
source of time during playback. A player may have at most one renderer that returns a
MediaClock
from this method.MediaClock
tracking the playback position of the renderer, or null.protected final int getState()
protected abstract boolean doPrepare(long positionUs) throws ExoPlaybackException
STATE_UNPREPARED
state. This
method will be called repeatedly until true
is returned.
This method should return quickly, and should not block if the renderer is currently unable to make any useful progress.
positionUs
- The player's current playback position.ExoPlaybackException
- If an error occurs.protected abstract int getTrackCount()
This method may be called when the renderer is in the following states:
STATE_PREPARED
, STATE_ENABLED
, STATE_STARTED
protected abstract MediaFormat getFormat(int track)
This method may be called when the renderer is in the following states:
STATE_PREPARED
, STATE_ENABLED
, STATE_STARTED
track
- The track index.protected void onEnabled(int track, long positionUs, boolean joining) throws ExoPlaybackException
The default implementation is a no-op.
track
- The track for which the renderer is being enabled.positionUs
- The player's current position.joining
- Whether this renderer is being enabled to join an ongoing playback.ExoPlaybackException
- If an error occurs.protected void onStarted() throws ExoPlaybackException
The default implementation is a no-op.
ExoPlaybackException
- If an error occurs.protected void onStopped() throws ExoPlaybackException
The default implementation is a no-op.
ExoPlaybackException
- If an error occurs.protected void onDisabled() throws ExoPlaybackException
The default implementation is a no-op.
ExoPlaybackException
- If an error occurs.protected void onReleased() throws ExoPlaybackException
The default implementation is a no-op.
ExoPlaybackException
- If an error occurs.protected abstract boolean isEnded()
ExoPlayer
instance to transition to
ExoPlayer.STATE_ENDED
. The player will make this transition as soon as true
is
returned by all of its TrackRenderer
s.
This method may be called when the renderer is in the following states:
STATE_ENABLED
, STATE_STARTED
protected abstract boolean isReady()
If the renderer is in the STATE_STARTED
state then returning true indicates that the
renderer has everything that it needs to continue playback. Returning false indicates that
the player should pause until the renderer is ready.
If the renderer is in the STATE_ENABLED
state then returning true indicates that the
renderer is ready for playback to be started. Returning false indicates that it is not.
This method may be called when the renderer is in the following states:
STATE_ENABLED
, STATE_STARTED
protected abstract void doSomeWork(long positionUs, long elapsedRealtimeUs) throws ExoPlaybackException
STATE_ENABLED
or
STATE_STARTED
states.
If the renderer's state is STATE_STARTED
, then repeated calls to this method should
cause the media track to be rendered. If the state is STATE_ENABLED
, then repeated
calls should make progress towards getting the renderer into a position where it is ready to
render the track.
This method should return quickly, and should not block if the renderer is currently unable to make any useful progress.
This method may be called when the renderer is in the following states:
STATE_ENABLED
, STATE_STARTED
positionUs
- The current media time in microseconds, measured at the start of the
current iteration of the rendering loop.elapsedRealtimeUs
- SystemClock.elapsedRealtime()
in microseconds,
measured at the start of the current iteration of the rendering loop.ExoPlaybackException
- If an error occurs.protected abstract void maybeThrowError() throws ExoPlaybackException
ExoPlaybackException
- An error that's preventing the renderer from making progress or
buffering more data.protected abstract long getDurationUs()
This method may be called when the renderer is in the following states:
STATE_PREPARED
, STATE_ENABLED
, STATE_STARTED
MATCH_LONGEST_US
if
the track's duration should match that of the longest track whose duration is known, or
or UNKNOWN_TIME_US
if the duration is not known.protected abstract long getBufferedPositionUs()
This method may be called when the renderer is in the following states:
STATE_ENABLED
, STATE_STARTED
END_OF_TRACK_US
if the track is fully buffered, or UNKNOWN_TIME_US
if
no estimate is available.protected abstract void seekTo(long positionUs) throws ExoPlaybackException
This method may be called when the renderer is in the following states:
STATE_ENABLED
positionUs
- The desired playback position in microseconds.ExoPlaybackException
- If an error occurs.public void handleMessage(int what, Object object) throws ExoPlaybackException
ExoPlayer.ExoPlayerComponent
handleMessage
in interface ExoPlayer.ExoPlayerComponent
what
- An integer identifying the type of message.object
- The message object.ExoPlaybackException
- If an error occurred whilst handling the message.