Add convenience methods player.next() and player.previous()

This simplifies code skipping items in a playlist programatically.

Issue:#4863

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=214580742
This commit is contained in:
tonihei 2018-09-26 03:27:52 -07:00 committed by Oliver Woodman
parent 083350b7db
commit b2e0a365d3
6 changed files with 131 additions and 0 deletions

View File

@ -8,6 +8,9 @@
* Fix an issue where audio and video would desynchronize when playing * Fix an issue where audio and video would desynchronize when playing
concatenations of gapless content concatenations of gapless content
([#4559](https://github.com/google/ExoPlayer/issues/4559)). ([#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 ### ### 2.9.0 ###

View File

@ -382,6 +382,32 @@ public final class CastPlayer implements Player {
} }
} }
@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 @Override
public void setPlaybackParameters(@Nullable PlaybackParameters playbackParameters) { public void setPlaybackParameters(@Nullable PlaybackParameters playbackParameters) {
// Unsupported by the RemoteMediaClient API. Do nothing. // Unsupported by the RemoteMediaClient API. Do nothing.

View File

@ -348,6 +348,32 @@ import java.util.concurrent.CopyOnWriteArraySet;
} }
} }
@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 @Override
public void setPlaybackParameters(@Nullable PlaybackParameters playbackParameters) { public void setPlaybackParameters(@Nullable PlaybackParameters playbackParameters) {
if (playbackParameters == null) { if (playbackParameters == null) {

View File

@ -655,6 +655,32 @@ public interface Player {
*/ */
void seekTo(int windowIndex, long positionMs); 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 * 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. * default, {@link PlaybackParameters#DEFAULT}, which means there is no speed or pitch adjustment.

View File

@ -955,6 +955,36 @@ public class SimpleExoPlayer
player.seekTo(windowIndex, 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 @Override
public void setPlaybackParameters(@Nullable PlaybackParameters playbackParameters) { public void setPlaybackParameters(@Nullable PlaybackParameters playbackParameters) {
verifyApplicationThread(); verifyApplicationThread();

View File

@ -149,6 +149,26 @@ public abstract class StubExoPlayer implements ExoPlayer {
throw new UnsupportedOperationException(); 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 @Override
public void setPlaybackParameters(PlaybackParameters playbackParameters) { public void setPlaybackParameters(PlaybackParameters playbackParameters) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();