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 bbd82cf5da
commit e4c20aa3de
6 changed files with 133 additions and 2 deletions

View File

@ -2,11 +2,14 @@
### 2.9.1 ### ### 2.9.1 ###
* Add convenience methods `Player.next`, `Player.previous`, `Player.hasNext`
and `Player.hasPrevious`
([#4863](https://github.com/google/ExoPlayer/issues/4863)).
* Improve initial bandwidth meter estimates using the current country and * Improve initial bandwidth meter estimates using the current country and
network type. network type.
* IMA extension: * IMA extension:
* For preroll to live stream transitions, project forward the * For preroll to live stream transitions, project forward the loading position
loading position to avoid being behind the live window. to avoid being behind the live window.
* Let apps specify whether to focus the skip button on ATV * Let apps specify whether to focus the skip button on ATV
([#5019](https://github.com/google/ExoPlayer/issues/5019)). ([#5019](https://github.com/google/ExoPlayer/issues/5019)).
* MP3: * MP3:

View File

@ -365,6 +365,32 @@ public final class CastPlayer extends BasePlayer {
} }
} }
@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

@ -329,6 +329,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

@ -659,6 +659,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

@ -934,6 +934,36 @@ public class SimpleExoPlayer extends BasePlayer
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

@ -134,6 +134,26 @@ public abstract class StubExoPlayer extends BasePlayer 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();