Skip to content

Commit

Permalink
Expose source indices via ExoPlayer (playlists #5).
Browse files Browse the repository at this point in the history
ExoPlayer.EventListener.onPositionDiscontinuity is notified during seeking and
transitioning from one source to the next.
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=125578976
  • Loading branch information
andrewlewis authored and ojw28 committed Jul 7, 2016
1 parent f9fa54c commit 2073f3f
Show file tree
Hide file tree
Showing 11 changed files with 224 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ public void onPlayerStateChanged(boolean playWhenReady, int state) {
+ getStateString(state) + "]");
}

@Override
public void onPositionDiscontinuity(int sourceIndex, long positionMs) {
Log.d(TAG, "discontinuity [" + sourceIndex + ", " + positionMs + "]");
}

@Override
public void onPlayWhenReadyCommitted() {
// Do nothing.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,14 @@ public void onPlayWhenReadyCommitted() {
// Do nothing.
}

@Override
public void onPositionDiscontinuity(int sourceIndex, long positionMs) {
if (mediaController.isShowing()) {
// The MediaController is visible, so force it to show the updated position immediately.
mediaController.show();
}
}

@Override
public void onPlayerError(ExoPlaybackException e) {
String errorString = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ public void onPlayWhenReadyCommitted () {
// Do nothing.
}

@Override
public void onPositionDiscontinuity(int sourceIndex, long positionMs) {
// Do nothing.
}

@Override
public void onPlayerError(ExoPlaybackException error) {
playbackException = error;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ public void onPlayWhenReadyCommitted () {
// Do nothing.
}

@Override
public void onPositionDiscontinuity(int sourceIndex, long positionMs) {
// Do nothing.
}

@Override
public void onPlayerError(ExoPlaybackException error) {
playbackException = error;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ public void onPlayWhenReadyCommitted () {
// Do nothing.
}

@Override
public void onPositionDiscontinuity(int sourceIndex, long positionMs) {
// Do nothing.
}

@Override
public void onPlayerError(ExoPlaybackException error) {
playbackException = error;
Expand Down
36 changes: 31 additions & 5 deletions library/src/main/java/com/google/android/exoplayer/ExoPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,16 @@ interface EventListener {
*/
void onPlayWhenReadyCommitted();

// TODO[playlists]: Should source-initiated resets also cause this to be invoked?
/**
* Invoked when the player's position changes due to a discontinuity (seeking or playback
* transitioning to the next source).
*
* @param sourceIndex The index of the source being played.
* @param positionMs The playback position in that source, in milliseconds.
*/
void onPositionDiscontinuity(int sourceIndex, long positionMs);

/**
* Invoked when an error occurs. The playback state will transition to
* {@link ExoPlayer#STATE_IDLE} immediately after this method is invoked. The player instance
Expand Down Expand Up @@ -227,8 +237,9 @@ public ExoPlayerMessage(ExoPlayerComponent target, int messageType, Object messa
void setSource(SampleSource sampleSource);

/**
* Sets the player's source provider. The player will transition to {@link #STATE_BUFFERING} until
* it is ready to play the first source.
* Sets the player's source provider. The player's position will be reset to the start of the
* first source and the player will transition to {@link #STATE_BUFFERING} until it is ready to
* play it.
*
* @param sourceProvider The provider of {@link SampleSource}s to play.
*/
Expand Down Expand Up @@ -259,12 +270,20 @@ public ExoPlayerMessage(ExoPlayerComponent target, int messageType, Object messa
boolean isPlayWhenReadyCommitted();

/**
* Seeks to a position specified in milliseconds.
* Seeks to a position specified in milliseconds in the current source.
*
* @param positionMs The seek position.
*/
void seekTo(long positionMs);

/**
* Seeks to a position specified in milliseconds in the specified source.
*
* @param sourceIndex The index of the source to seek to.
* @param positionMs The seek position relative to the start of the specified source.
*/
void seekTo(int sourceIndex, long positionMs);

/**
* Stops playback. Use {@code setPlayWhenReady(false)} rather than this method if the intention
* is to pause playback.
Expand Down Expand Up @@ -312,12 +331,19 @@ public ExoPlayerMessage(ExoPlayerComponent target, int messageType, Object messa
long getDuration();

/**
* Gets the current playback position in milliseconds.
* Gets the playback position in the current source, in milliseconds.
*
* @return The current playback position in milliseconds.
* @return The playback position in the current source, in milliseconds.
*/
long getCurrentPosition();

/**
* Gets the index of the current source.
*
* @return The index of the current source.
*/
int getCurrentSourceIndex();

/**
* Gets an estimate of the absolute position in milliseconds up to which data is buffered.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@
private boolean playWhenReady;
private int playbackState;
private int pendingPlayWhenReadyAcks;
private int pendingSetSourceProviderAndSeekAcks;

// Playback information when there is no pending seek/set source operation.
private ExoPlayerImplInternal.PlaybackInfo playbackInfo;

// Playback information when there is a pending seek/set source operation.
private int sourceIndex;
private long position;
private long duration;

/**
* Constructs an instance. Must be invoked from a thread that has an associated {@link Looper}.
Expand Down Expand Up @@ -68,6 +77,7 @@ public void handleMessage(Message msg) {
};
internalPlayer = new ExoPlayerImplInternal(renderers, trackSelector, minBufferMs, minRebufferMs,
playWhenReady, eventHandler);
playbackInfo = new ExoPlayerImplInternal.PlaybackInfo(0);
}

@Override
Expand All @@ -87,12 +97,20 @@ public int getPlaybackState() {

@Override
public void setSource(final SampleSource sampleSource) {
internalPlayer.setSourceProvider(new SingleSampleSourceProvider(sampleSource));
setSourceProvider(new SingleSampleSourceProvider(sampleSource));
}

@Override
public void setSourceProvider(SampleSourceProvider sourceProvider) {
duration = ExoPlayer.UNKNOWN_TIME;
position = 0;
sourceIndex = 0;

pendingSetSourceProviderAndSeekAcks++;
internalPlayer.setSourceProvider(sourceProvider);
for (EventListener listener : listeners) {
listener.onPositionDiscontinuity(sourceIndex, position);
}
}

@Override
Expand All @@ -119,7 +137,20 @@ public boolean isPlayWhenReadyCommitted() {

@Override
public void seekTo(long positionMs) {
internalPlayer.seekTo(positionMs);
seekTo(getCurrentSourceIndex(), positionMs);
}

@Override
public void seekTo(int sourceIndex, long positionMs) {
duration = sourceIndex == getCurrentSourceIndex() ? getDuration() : ExoPlayer.UNKNOWN_TIME;
position = positionMs;
this.sourceIndex = sourceIndex;

pendingSetSourceProviderAndSeekAcks++;
internalPlayer.seekTo(sourceIndex, position);
for (EventListener listener : listeners) {
listener.onPositionDiscontinuity(sourceIndex, position);
}
}

@Override
Expand All @@ -145,17 +176,33 @@ public void blockingSendMessages(ExoPlayerMessage... messages) {

@Override
public long getDuration() {
return internalPlayer.getDuration();
if (pendingSetSourceProviderAndSeekAcks == 0) {
long durationUs = playbackInfo.durationUs;
return durationUs == C.UNSET_TIME_US ? ExoPlayer.UNKNOWN_TIME : durationUs / 1000;
} else {
return duration;
}
}

@Override
public long getCurrentPosition() {
return internalPlayer.getCurrentPosition();
return pendingSetSourceProviderAndSeekAcks == 0 ? playbackInfo.positionUs / 1000 : position;
}

@Override
public int getCurrentSourceIndex() {
return pendingSetSourceProviderAndSeekAcks == 0 ? playbackInfo.sourceIndex : sourceIndex;
}

@Override
public long getBufferedPosition() {
return internalPlayer.getBufferedPosition();
if (pendingSetSourceProviderAndSeekAcks == 0) {
long bufferedPositionUs = playbackInfo.bufferedPositionUs;
return bufferedPositionUs == C.UNSET_TIME_US || bufferedPositionUs == C.END_OF_SOURCE_US
? ExoPlayer.UNKNOWN_TIME : bufferedPositionUs / 1000;
} else {
return position;
}
}

@Override
Expand Down Expand Up @@ -185,6 +232,20 @@ public int getBufferedPercentage() {
}
break;
}
case ExoPlayerImplInternal.MSG_SET_SOURCE_PROVIDER_ACK: // Fall through.
case ExoPlayerImplInternal.MSG_SEEK_ACK: {
pendingSetSourceProviderAndSeekAcks--;
break;
}
case ExoPlayerImplInternal.MSG_SOURCE_CHANGED: {
playbackInfo = (ExoPlayerImplInternal.PlaybackInfo) msg.obj;
if (pendingSetSourceProviderAndSeekAcks == 0) {
for (EventListener listener : listeners) {
listener.onPositionDiscontinuity(playbackInfo.sourceIndex, 0);
}
}
break;
}
case ExoPlayerImplInternal.MSG_ERROR: {
ExoPlaybackException exception = (ExoPlaybackException) msg.obj;
for (EventListener listener : listeners) {
Expand Down
Loading

0 comments on commit 2073f3f

Please sign in to comment.