Skip to content

Commit

Permalink
Add convenience methods player.next() and player.previous()
Browse files Browse the repository at this point in the history
This simplifies code skipping items in a playlist programatically.

Issue:#4863

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=214580742
  • Loading branch information
tonihei authored and ojw28 committed Sep 26, 2018
1 parent 083350b commit b2e0a36
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 0 deletions.
3 changes: 3 additions & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
* Fix an issue where audio and video would desynchronize when playing
concatenations of gapless content
([#4559](https://github.com/google/ExoPlayer/issues/4559)).
* Add convenience methods `Player.next`, `Player.previous`, `Player.hasNext`
and `Player.hasPrevious`
([#4863](https://github.com/google/ExoPlayer/issues/4863)).

### 2.9.0 ###

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,32 @@ public void seekTo(int windowIndex, long positionMs) {
}
}

@Override
public boolean hasPrevious() {
return getPreviousWindowIndex() != C.INDEX_UNSET;
}

@Override
public void previous() {
int previousWindowIndex = getPreviousWindowIndex();
if (previousWindowIndex != C.INDEX_UNSET) {
seekToDefaultPosition(previousWindowIndex);
}
}

@Override
public boolean hasNext() {
return getNextWindowIndex() != C.INDEX_UNSET;
}

@Override
public void next() {
int nextWindowIndex = getPreviousWindowIndex();
if (nextWindowIndex != C.INDEX_UNSET) {
seekToDefaultPosition(nextWindowIndex);
}
}

@Override
public void setPlaybackParameters(@Nullable PlaybackParameters playbackParameters) {
// Unsupported by the RemoteMediaClient API. Do nothing.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,32 @@ public void seekTo(int windowIndex, long positionMs) {
}
}

@Override
public boolean hasPrevious() {
return getPreviousWindowIndex() != C.INDEX_UNSET;
}

@Override
public void previous() {
int previousWindowIndex = getPreviousWindowIndex();
if (previousWindowIndex != C.INDEX_UNSET) {
seekToDefaultPosition(previousWindowIndex);
}
}

@Override
public boolean hasNext() {
return getNextWindowIndex() != C.INDEX_UNSET;
}

@Override
public void next() {
int nextWindowIndex = getPreviousWindowIndex();
if (nextWindowIndex != C.INDEX_UNSET) {
seekToDefaultPosition(nextWindowIndex);
}
}

@Override
public void setPlaybackParameters(@Nullable PlaybackParameters playbackParameters) {
if (playbackParameters == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,32 @@ public void onTimelineChanged(Timeline timeline, @Nullable Object manifest) {
*/
void seekTo(int windowIndex, long positionMs);

/**
* Returns whether a previous window exists, which may depend on the current repeat mode and
* whether shuffle mode is enabled.
*/
boolean hasPrevious();

/**
* Seeks to the default position of the previous window in the timeline, which may depend on the
* current repeat mode and whether shuffle mode is enabled. Does nothing if {@link #hasPrevious()}
* is {@code false}.
*/
void previous();

/**
* Returns whether a next window exists, which may depend on the current repeat mode and whether
* shuffle mode is enabled.
*/
boolean hasNext();

/**
* Seeks to the default position of the next window in the timeline, which may depend on the
* current repeat mode and whether shuffle mode is enabled. Does nothing if {@link #hasNext()} is
* {@code false}.
*/
void next();

/**
* Attempts to set the playback parameters. Passing {@code null} sets the parameters to the
* default, {@link PlaybackParameters#DEFAULT}, which means there is no speed or pitch adjustment.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,36 @@ public void seekTo(int windowIndex, long positionMs) {
player.seekTo(windowIndex, positionMs);
}

@Override
public boolean hasPrevious() {
verifyApplicationThread();
return getPreviousWindowIndex() != C.INDEX_UNSET;
}

@Override
public void previous() {
verifyApplicationThread();
if (hasPrevious()) {
analyticsCollector.notifySeekStarted();
player.previous();
}
}

@Override
public boolean hasNext() {
verifyApplicationThread();
return getNextWindowIndex() != C.INDEX_UNSET;
}

@Override
public void next() {
verifyApplicationThread();
if (hasNext()) {
analyticsCollector.notifySeekStarted();
player.next();
}
}

@Override
public void setPlaybackParameters(@Nullable PlaybackParameters playbackParameters) {
verifyApplicationThread();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,26 @@ public void seekTo(int windowIndex, long positionMs) {
throw new UnsupportedOperationException();
}

@Override
public boolean hasPrevious() {
throw new UnsupportedOperationException();
}

@Override
public void previous() {
throw new UnsupportedOperationException();
}

@Override
public boolean hasNext() {
throw new UnsupportedOperationException();
}

@Override
public void next() {
throw new UnsupportedOperationException();
}

@Override
public void setPlaybackParameters(PlaybackParameters playbackParameters) {
throw new UnsupportedOperationException();
Expand Down

0 comments on commit b2e0a36

Please sign in to comment.