Commit 38308488 by kimvde Committed by Ian Baker

Use Player methods in DefaultControlDispatcher

PiperOrigin-RevId: 385118021
parent f173ffa9
...@@ -98,6 +98,8 @@ ...@@ -98,6 +98,8 @@
corresponding buttons can be disabled by using a `ForwardingPlayer` corresponding buttons can be disabled by using a `ForwardingPlayer`
that removes `COMMAND_SEEK_BACK` and `COMMAND_SEEK_FORWARD` from the that removes `COMMAND_SEEK_BACK` and `COMMAND_SEEK_FORWARD` from the
available commands. available commands.
* Update `DefaultControlDispatcher` `getRewindIncrementMs` and
`getFastForwardIncrementMs` to take the player as parameter.
* Video: * Video:
* Fix `IncorrectContextUseViolation` strict mode warning on Android 11 * Fix `IncorrectContextUseViolation` strict mode warning on Android 11
([#8246](https://github.com/google/ExoPlayer/pull/8246)). ([#8246](https://github.com/google/ExoPlayer/pull/8246)).
......
...@@ -21,19 +21,15 @@ import static java.lang.Math.min; ...@@ -21,19 +21,15 @@ import static java.lang.Math.min;
/** Default {@link ControlDispatcher}. */ /** Default {@link ControlDispatcher}. */
public class DefaultControlDispatcher implements ControlDispatcher { public class DefaultControlDispatcher implements ControlDispatcher {
/** The default fast forward increment, in milliseconds. */
public static final int DEFAULT_FAST_FORWARD_MS = 15_000;
/** The default rewind increment, in milliseconds. */
public static final int DEFAULT_REWIND_MS = 5000;
private static final int MAX_POSITION_FOR_SEEK_TO_PREVIOUS = 3000;
private final long rewindIncrementMs; private final long rewindIncrementMs;
private final long fastForwardIncrementMs; private final long fastForwardIncrementMs;
private final boolean rewindAndFastForwardIncrementsSet;
/** Creates an instance. */ /** Creates an instance. */
public DefaultControlDispatcher() { public DefaultControlDispatcher() {
this(DEFAULT_FAST_FORWARD_MS, DEFAULT_REWIND_MS); fastForwardIncrementMs = C.TIME_UNSET;
rewindIncrementMs = C.TIME_UNSET;
rewindAndFastForwardIncrementsSet = false;
} }
/** /**
...@@ -47,6 +43,7 @@ public class DefaultControlDispatcher implements ControlDispatcher { ...@@ -47,6 +43,7 @@ public class DefaultControlDispatcher implements ControlDispatcher {
public DefaultControlDispatcher(long fastForwardIncrementMs, long rewindIncrementMs) { public DefaultControlDispatcher(long fastForwardIncrementMs, long rewindIncrementMs) {
this.fastForwardIncrementMs = fastForwardIncrementMs; this.fastForwardIncrementMs = fastForwardIncrementMs;
this.rewindIncrementMs = rewindIncrementMs; this.rewindIncrementMs = rewindIncrementMs;
rewindAndFastForwardIncrementsSet = true;
} }
@Override @Override
...@@ -69,39 +66,21 @@ public class DefaultControlDispatcher implements ControlDispatcher { ...@@ -69,39 +66,21 @@ public class DefaultControlDispatcher implements ControlDispatcher {
@Override @Override
public boolean dispatchPrevious(Player player) { public boolean dispatchPrevious(Player player) {
Timeline timeline = player.getCurrentTimeline(); player.seekToPrevious();
if (timeline.isEmpty() || player.isPlayingAd()) {
return true;
}
boolean isUnseekableLiveStream =
player.isCurrentWindowLive() && !player.isCurrentWindowSeekable();
if (player.hasPreviousWindow()
&& (player.getCurrentPosition() <= MAX_POSITION_FOR_SEEK_TO_PREVIOUS
|| isUnseekableLiveStream)) {
player.seekToPreviousWindow();
} else if (!isUnseekableLiveStream) {
player.seekTo(/* positionMs= */ 0);
}
return true; return true;
} }
@Override @Override
public boolean dispatchNext(Player player) { public boolean dispatchNext(Player player) {
Timeline timeline = player.getCurrentTimeline(); player.seekToNext();
if (timeline.isEmpty() || player.isPlayingAd()) {
return true;
}
if (player.hasNextWindow()) {
player.seekToNextWindow();
} else if (player.isCurrentWindowLive() && player.isCurrentWindowDynamic()) {
player.seekToDefaultPosition();
}
return true; return true;
} }
@Override @Override
public boolean dispatchRewind(Player player) { public boolean dispatchRewind(Player player) {
if (isRewindEnabled() && player.isCurrentWindowSeekable()) { if (!rewindAndFastForwardIncrementsSet) {
player.seekBack();
} else if (isRewindEnabled() && player.isCurrentWindowSeekable()) {
seekToOffset(player, -rewindIncrementMs); seekToOffset(player, -rewindIncrementMs);
} }
return true; return true;
...@@ -109,7 +88,9 @@ public class DefaultControlDispatcher implements ControlDispatcher { ...@@ -109,7 +88,9 @@ public class DefaultControlDispatcher implements ControlDispatcher {
@Override @Override
public boolean dispatchFastForward(Player player) { public boolean dispatchFastForward(Player player) {
if (isFastForwardEnabled() && player.isCurrentWindowSeekable()) { if (!rewindAndFastForwardIncrementsSet) {
player.seekForward();
} else if (isFastForwardEnabled() && player.isCurrentWindowSeekable()) {
seekToOffset(player, fastForwardIncrementMs); seekToOffset(player, fastForwardIncrementMs);
} }
return true; return true;
...@@ -142,22 +123,24 @@ public class DefaultControlDispatcher implements ControlDispatcher { ...@@ -142,22 +123,24 @@ public class DefaultControlDispatcher implements ControlDispatcher {
@Override @Override
public boolean isRewindEnabled() { public boolean isRewindEnabled() {
return rewindIncrementMs > 0; return !rewindAndFastForwardIncrementsSet || rewindIncrementMs > 0;
} }
@Override @Override
public boolean isFastForwardEnabled() { public boolean isFastForwardEnabled() {
return fastForwardIncrementMs > 0; return !rewindAndFastForwardIncrementsSet || fastForwardIncrementMs > 0;
} }
/** Returns the rewind increment in milliseconds. */ /** Returns the rewind increment in milliseconds. */
public long getRewindIncrementMs() { public long getRewindIncrementMs(Player player) {
return rewindIncrementMs; return rewindAndFastForwardIncrementsSet ? rewindIncrementMs : player.getSeekBackIncrement();
} }
/** Returns the fast forward increment in milliseconds. */ /** Returns the fast forward increment in milliseconds. */
public long getFastForwardIncrementMs() { public long getFastForwardIncrementMs(Player player) {
return fastForwardIncrementMs; return rewindAndFastForwardIncrementsSet
? fastForwardIncrementMs
: player.getSeekForwardIncrement();
} }
// Internal methods. // Internal methods.
......
...@@ -26,6 +26,8 @@ import static com.google.android.exoplayer2.Player.EVENT_PLAYBACK_STATE_CHANGED; ...@@ -26,6 +26,8 @@ import static com.google.android.exoplayer2.Player.EVENT_PLAYBACK_STATE_CHANGED;
import static com.google.android.exoplayer2.Player.EVENT_PLAY_WHEN_READY_CHANGED; import static com.google.android.exoplayer2.Player.EVENT_PLAY_WHEN_READY_CHANGED;
import static com.google.android.exoplayer2.Player.EVENT_POSITION_DISCONTINUITY; import static com.google.android.exoplayer2.Player.EVENT_POSITION_DISCONTINUITY;
import static com.google.android.exoplayer2.Player.EVENT_REPEAT_MODE_CHANGED; import static com.google.android.exoplayer2.Player.EVENT_REPEAT_MODE_CHANGED;
import static com.google.android.exoplayer2.Player.EVENT_SEEK_BACK_INCREMENT_CHANGED;
import static com.google.android.exoplayer2.Player.EVENT_SEEK_FORWARD_INCREMENT_CHANGED;
import static com.google.android.exoplayer2.Player.EVENT_SHUFFLE_MODE_ENABLED_CHANGED; import static com.google.android.exoplayer2.Player.EVENT_SHUFFLE_MODE_ENABLED_CHANGED;
import static com.google.android.exoplayer2.Player.EVENT_TIMELINE_CHANGED; import static com.google.android.exoplayer2.Player.EVENT_TIMELINE_CHANGED;
import static com.google.android.exoplayer2.Player.EVENT_TRACKS_CHANGED; import static com.google.android.exoplayer2.Player.EVENT_TRACKS_CHANGED;
...@@ -1142,8 +1144,8 @@ public class StyledPlayerControlView extends FrameLayout { ...@@ -1142,8 +1144,8 @@ public class StyledPlayerControlView extends FrameLayout {
private void updateRewindButton() { private void updateRewindButton() {
long rewindMs = long rewindMs =
controlDispatcher instanceof DefaultControlDispatcher controlDispatcher instanceof DefaultControlDispatcher && player != null
? ((DefaultControlDispatcher) controlDispatcher).getRewindIncrementMs() ? ((DefaultControlDispatcher) controlDispatcher).getRewindIncrementMs(player)
: C.DEFAULT_SEEK_BACK_INCREMENT_MS; : C.DEFAULT_SEEK_BACK_INCREMENT_MS;
int rewindSec = (int) (rewindMs / 1_000); int rewindSec = (int) (rewindMs / 1_000);
if (rewindButtonTextView != null) { if (rewindButtonTextView != null) {
...@@ -1158,8 +1160,8 @@ public class StyledPlayerControlView extends FrameLayout { ...@@ -1158,8 +1160,8 @@ public class StyledPlayerControlView extends FrameLayout {
private void updateFastForwardButton() { private void updateFastForwardButton() {
long fastForwardMs = long fastForwardMs =
controlDispatcher instanceof DefaultControlDispatcher controlDispatcher instanceof DefaultControlDispatcher && player != null
? ((DefaultControlDispatcher) controlDispatcher).getFastForwardIncrementMs() ? ((DefaultControlDispatcher) controlDispatcher).getFastForwardIncrementMs(player)
: C.DEFAULT_SEEK_FORWARD_INCREMENT_MS; : C.DEFAULT_SEEK_FORWARD_INCREMENT_MS;
int fastForwardSec = (int) (fastForwardMs / 1_000); int fastForwardSec = (int) (fastForwardMs / 1_000);
if (fastForwardButtonTextView != null) { if (fastForwardButtonTextView != null) {
...@@ -1756,7 +1758,9 @@ public class StyledPlayerControlView extends FrameLayout { ...@@ -1756,7 +1758,9 @@ public class StyledPlayerControlView extends FrameLayout {
EVENT_REPEAT_MODE_CHANGED, EVENT_REPEAT_MODE_CHANGED,
EVENT_SHUFFLE_MODE_ENABLED_CHANGED, EVENT_SHUFFLE_MODE_ENABLED_CHANGED,
EVENT_POSITION_DISCONTINUITY, EVENT_POSITION_DISCONTINUITY,
EVENT_TIMELINE_CHANGED)) { EVENT_TIMELINE_CHANGED,
EVENT_SEEK_BACK_INCREMENT_CHANGED,
EVENT_SEEK_FORWARD_INCREMENT_CHANGED)) {
updateNavigation(); updateNavigation();
} }
if (events.containsAny(EVENT_POSITION_DISCONTINUITY, EVENT_TIMELINE_CHANGED)) { if (events.containsAny(EVENT_POSITION_DISCONTINUITY, EVENT_TIMELINE_CHANGED)) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment