Commit e1c48515 by bachinger Committed by kim-vde

add next, previous, fastForward and rewind to ControlDispatcher

Issue: #6926, #6934
PiperOrigin-RevId: 293315532
parent 0f6bf152
...@@ -39,6 +39,9 @@ ...@@ -39,6 +39,9 @@
* Testing * Testing
* Upgrade Truth dependency from 0.44 to 1.0. * Upgrade Truth dependency from 0.44 to 1.0.
* Upgrade to JUnit 4.13-rc-2. * Upgrade to JUnit 4.13-rc-2.
* UI
* move logic of prev, next, fast forward and rewind to ControlDispatcher
[#6926](https://github.com/google/ExoPlayer/issues/6926)).
### 2.11.2 (TBD) ### ### 2.11.2 (TBD) ###
......
...@@ -127,11 +127,6 @@ public final class MediaSessionConnector { ...@@ -127,11 +127,6 @@ public final class MediaSessionConnector {
/** The default playback actions. */ /** The default playback actions. */
@PlaybackActions public static final long DEFAULT_PLAYBACK_ACTIONS = ALL_PLAYBACK_ACTIONS; @PlaybackActions public static final long DEFAULT_PLAYBACK_ACTIONS = ALL_PLAYBACK_ACTIONS;
/** The default fast forward increment, in milliseconds. */
public static final int DEFAULT_FAST_FORWARD_MS = 15000;
/** The default rewind increment, in milliseconds. */
public static final int DEFAULT_REWIND_MS = 5000;
/** /**
* The name of the {@link PlaybackStateCompat} float extra with the value of {@link * The name of the {@link PlaybackStateCompat} float extra with the value of {@link
* PlaybackParameters#speed}. * PlaybackParameters#speed}.
...@@ -440,8 +435,6 @@ public final class MediaSessionConnector { ...@@ -440,8 +435,6 @@ public final class MediaSessionConnector {
@Nullable private MediaButtonEventHandler mediaButtonEventHandler; @Nullable private MediaButtonEventHandler mediaButtonEventHandler;
private long enabledPlaybackActions; private long enabledPlaybackActions;
private int rewindMs;
private int fastForwardMs;
/** /**
* Creates an instance. * Creates an instance.
...@@ -461,8 +454,6 @@ public final class MediaSessionConnector { ...@@ -461,8 +454,6 @@ public final class MediaSessionConnector {
new DefaultMediaMetadataProvider( new DefaultMediaMetadataProvider(
mediaSession.getController(), /* metadataExtrasPrefix= */ null); mediaSession.getController(), /* metadataExtrasPrefix= */ null);
enabledPlaybackActions = DEFAULT_PLAYBACK_ACTIONS; enabledPlaybackActions = DEFAULT_PLAYBACK_ACTIONS;
rewindMs = DEFAULT_REWIND_MS;
fastForwardMs = DEFAULT_FAST_FORWARD_MS;
mediaSession.setFlags(BASE_MEDIA_SESSION_FLAGS); mediaSession.setFlags(BASE_MEDIA_SESSION_FLAGS);
mediaSession.setCallback(componentListener, new Handler(looper)); mediaSession.setCallback(componentListener, new Handler(looper));
} }
...@@ -504,13 +495,12 @@ public final class MediaSessionConnector { ...@@ -504,13 +495,12 @@ public final class MediaSessionConnector {
/** /**
* Sets the {@link ControlDispatcher}. * Sets the {@link ControlDispatcher}.
* *
* @param controlDispatcher The {@link ControlDispatcher}, or null to use {@link * @param controlDispatcher The {@link ControlDispatcher}.
* DefaultControlDispatcher}.
*/ */
public void setControlDispatcher(@Nullable ControlDispatcher controlDispatcher) { public void setControlDispatcher(ControlDispatcher controlDispatcher) {
if (this.controlDispatcher != controlDispatcher) { if (this.controlDispatcher != controlDispatcher) {
this.controlDispatcher = this.controlDispatcher = controlDispatcher;
controlDispatcher == null ? new DefaultControlDispatcher() : controlDispatcher; invalidateMediaSessionPlaybackState();
} }
} }
...@@ -551,27 +541,27 @@ public final class MediaSessionConnector { ...@@ -551,27 +541,27 @@ public final class MediaSessionConnector {
} }
/** /**
* Sets the rewind increment in milliseconds. * @deprecated Use {@link #setControlDispatcher(ControlDispatcher)} with {@link
* * DefaultControlDispatcher#DefaultControlDispatcher(long, long)} instead.
* @param rewindMs The rewind increment in milliseconds. A non-positive value will cause the
* rewind button to be disabled.
*/ */
@SuppressWarnings("deprecation")
@Deprecated
public void setRewindIncrementMs(int rewindMs) { public void setRewindIncrementMs(int rewindMs) {
if (this.rewindMs != rewindMs) { if (controlDispatcher instanceof DefaultControlDispatcher) {
this.rewindMs = rewindMs; ((DefaultControlDispatcher) controlDispatcher).setRewindIncrementMs(rewindMs);
invalidateMediaSessionPlaybackState(); invalidateMediaSessionPlaybackState();
} }
} }
/** /**
* Sets the fast forward increment in milliseconds. * @deprecated Use {@link #setControlDispatcher(ControlDispatcher)} with {@link
* * DefaultControlDispatcher#DefaultControlDispatcher(long, long)} instead.
* @param fastForwardMs The fast forward increment in milliseconds. A non-positive value will
* cause the fast forward button to be disabled.
*/ */
@SuppressWarnings("deprecation")
@Deprecated
public void setFastForwardIncrementMs(int fastForwardMs) { public void setFastForwardIncrementMs(int fastForwardMs) {
if (this.fastForwardMs != fastForwardMs) { if (controlDispatcher instanceof DefaultControlDispatcher) {
this.fastForwardMs = fastForwardMs; ((DefaultControlDispatcher) controlDispatcher).setFastForwardIncrementMs(fastForwardMs);
invalidateMediaSessionPlaybackState(); invalidateMediaSessionPlaybackState();
} }
} }
...@@ -875,8 +865,8 @@ public final class MediaSessionConnector { ...@@ -875,8 +865,8 @@ public final class MediaSessionConnector {
Timeline timeline = player.getCurrentTimeline(); Timeline timeline = player.getCurrentTimeline();
if (!timeline.isEmpty() && !player.isPlayingAd()) { if (!timeline.isEmpty() && !player.isPlayingAd()) {
enableSeeking = player.isCurrentWindowSeekable(); enableSeeking = player.isCurrentWindowSeekable();
enableRewind = enableSeeking && rewindMs > 0; enableRewind = enableSeeking && controlDispatcher.isRewindEnabled();
enableFastForward = enableSeeking && fastForwardMs > 0; enableFastForward = enableSeeking && controlDispatcher.isFastForwardEnabled();
enableSetRating = ratingCallback != null; enableSetRating = ratingCallback != null;
enableSetCaptioningEnabled = captionCallback != null && captionCallback.hasCaptions(player); enableSetCaptioningEnabled = captionCallback != null && captionCallback.hasCaptions(player);
} }
...@@ -955,28 +945,6 @@ public final class MediaSessionConnector { ...@@ -955,28 +945,6 @@ public final class MediaSessionConnector {
return player != null && mediaButtonEventHandler != null; return player != null && mediaButtonEventHandler != null;
} }
private void rewind(Player player) {
if (player.isCurrentWindowSeekable() && rewindMs > 0) {
seekToOffset(player, /* offsetMs= */ -rewindMs);
}
}
private void fastForward(Player player) {
if (player.isCurrentWindowSeekable() && fastForwardMs > 0) {
seekToOffset(player, /* offsetMs= */ fastForwardMs);
}
}
private void seekToOffset(Player player, long offsetMs) {
long positionMs = player.getCurrentPosition() + offsetMs;
long durationMs = player.getDuration();
if (durationMs != C.TIME_UNSET) {
positionMs = Math.min(positionMs, durationMs);
}
positionMs = Math.max(positionMs, 0);
seekTo(player, player.getCurrentWindowIndex(), positionMs);
}
private void seekTo(Player player, int windowIndex, long positionMs) { private void seekTo(Player player, int windowIndex, long positionMs) {
controlDispatcher.dispatchSeekTo(player, windowIndex, positionMs); controlDispatcher.dispatchSeekTo(player, windowIndex, positionMs);
} }
...@@ -1203,14 +1171,14 @@ public final class MediaSessionConnector { ...@@ -1203,14 +1171,14 @@ public final class MediaSessionConnector {
@Override @Override
public void onFastForward() { public void onFastForward() {
if (canDispatchPlaybackAction(PlaybackStateCompat.ACTION_FAST_FORWARD)) { if (canDispatchPlaybackAction(PlaybackStateCompat.ACTION_FAST_FORWARD)) {
fastForward(player); controlDispatcher.dispatchFastForward(player);
} }
} }
@Override @Override
public void onRewind() { public void onRewind() {
if (canDispatchPlaybackAction(PlaybackStateCompat.ACTION_REWIND)) { if (canDispatchPlaybackAction(PlaybackStateCompat.ACTION_REWIND)) {
rewind(player); controlDispatcher.dispatchRewind(player);
} }
} }
......
...@@ -36,7 +36,6 @@ import java.util.Collections; ...@@ -36,7 +36,6 @@ import java.util.Collections;
*/ */
public abstract class TimelineQueueNavigator implements MediaSessionConnector.QueueNavigator { public abstract class TimelineQueueNavigator implements MediaSessionConnector.QueueNavigator {
public static final long MAX_POSITION_FOR_SEEK_TO_PREVIOUS = 3000;
public static final int DEFAULT_MAX_QUEUE_SIZE = 10; public static final int DEFAULT_MAX_QUEUE_SIZE = 10;
private final MediaSessionCompat mediaSession; private final MediaSessionCompat mediaSession;
...@@ -136,20 +135,7 @@ public abstract class TimelineQueueNavigator implements MediaSessionConnector.Qu ...@@ -136,20 +135,7 @@ public abstract class TimelineQueueNavigator implements MediaSessionConnector.Qu
@Override @Override
public void onSkipToPrevious(Player player, ControlDispatcher controlDispatcher) { public void onSkipToPrevious(Player player, ControlDispatcher controlDispatcher) {
Timeline timeline = player.getCurrentTimeline(); controlDispatcher.dispatchPrevious(player);
if (timeline.isEmpty() || player.isPlayingAd()) {
return;
}
int windowIndex = player.getCurrentWindowIndex();
timeline.getWindow(windowIndex, window);
int previousWindowIndex = player.getPreviousWindowIndex();
if (previousWindowIndex != C.INDEX_UNSET
&& (player.getCurrentPosition() <= MAX_POSITION_FOR_SEEK_TO_PREVIOUS
|| (window.isDynamic && !window.isSeekable))) {
controlDispatcher.dispatchSeekTo(player, previousWindowIndex, C.TIME_UNSET);
} else {
controlDispatcher.dispatchSeekTo(player, windowIndex, 0);
}
} }
@Override @Override
...@@ -166,17 +152,7 @@ public abstract class TimelineQueueNavigator implements MediaSessionConnector.Qu ...@@ -166,17 +152,7 @@ public abstract class TimelineQueueNavigator implements MediaSessionConnector.Qu
@Override @Override
public void onSkipToNext(Player player, ControlDispatcher controlDispatcher) { public void onSkipToNext(Player player, ControlDispatcher controlDispatcher) {
Timeline timeline = player.getCurrentTimeline(); controlDispatcher.dispatchNext(player);
if (timeline.isEmpty() || player.isPlayingAd()) {
return;
}
int windowIndex = player.getCurrentWindowIndex();
int nextWindowIndex = player.getNextWindowIndex();
if (nextWindowIndex != C.INDEX_UNSET) {
controlDispatcher.dispatchSeekTo(player, nextWindowIndex, C.TIME_UNSET);
} else if (timeline.getWindow(windowIndex, window).isDynamic) {
controlDispatcher.dispatchSeekTo(player, windowIndex, C.TIME_UNSET);
}
} }
// CommandReceiver implementation. // CommandReceiver implementation.
......
...@@ -47,6 +47,38 @@ public interface ControlDispatcher { ...@@ -47,6 +47,38 @@ public interface ControlDispatcher {
boolean dispatchSeekTo(Player player, int windowIndex, long positionMs); boolean dispatchSeekTo(Player player, int windowIndex, long positionMs);
/** /**
* Dispatches a {@link Player#previous()} operation.
*
* @param player The {@link Player} to which the operation should be dispatched.
* @return True if the operation was dispatched. False if suppressed.
*/
boolean dispatchPrevious(Player player);
/**
* Dispatches a {@link Player#next()} operation.
*
* @param player The {@link Player} to which the operation should be dispatched.
* @return True if the operation was dispatched. False if suppressed.
*/
boolean dispatchNext(Player player);
/**
* Dispatches a rewind operation.
*
* @param player The {@link Player} to which the operation should be dispatched.
* @return True if the operation was dispatched. False if suppressed.
*/
boolean dispatchRewind(Player player);
/**
* Dispatches a fast forward operation.
*
* @param player The {@link Player} to which the operation should be dispatched.
* @return True if the operation was dispatched. False if suppressed.
*/
boolean dispatchFastForward(Player player);
/**
* Dispatches a {@link Player#setRepeatMode(int)} operation. * Dispatches a {@link Player#setRepeatMode(int)} operation.
* *
* @param player The {@link Player} to which the operation should be dispatched. * @param player The {@link Player} to which the operation should be dispatched.
...@@ -72,4 +104,10 @@ public interface ControlDispatcher { ...@@ -72,4 +104,10 @@ public interface ControlDispatcher {
* @return True if the operation was dispatched. False if suppressed. * @return True if the operation was dispatched. False if suppressed.
*/ */
boolean dispatchStop(Player player, boolean reset); boolean dispatchStop(Player player, boolean reset);
/** Returns {@code true} if rewind is enabled, {@code false} otherwise. */
boolean isRewindEnabled();
/** Returns {@code true} if fast forward is enabled, {@code false} otherwise. */
boolean isFastForwardEnabled();
} }
...@@ -15,14 +15,40 @@ ...@@ -15,14 +15,40 @@
*/ */
package com.google.android.exoplayer2; package com.google.android.exoplayer2;
import com.google.android.exoplayer2.Player.RepeatMode; /** Default {@link ControlDispatcher}. */
/**
* Default {@link ControlDispatcher} that dispatches all operations to the player without
* modification.
*/
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 = 15000;
/** 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 Timeline.Window window;
private long rewindIncrementMs;
private long fastForwardIncrementMs;
/** Creates an instance. */
public DefaultControlDispatcher() {
this(DEFAULT_FAST_FORWARD_MS, DEFAULT_REWIND_MS);
}
/**
* Creates an instance with the given increments.
*
* @param fastForwardIncrementMs The fast forward increment in milliseconds. A non-positive value
* disables the fast forward operation.
* @param rewindIncrementMs The rewind increment in milliseconds. A non-positive value disables
* the rewind operation.
*/
public DefaultControlDispatcher(long fastForwardIncrementMs, long rewindIncrementMs) {
this.fastForwardIncrementMs = fastForwardIncrementMs;
this.rewindIncrementMs = rewindIncrementMs;
window = new Timeline.Window();
}
@Override @Override
public boolean dispatchSetPlayWhenReady(Player player, boolean playWhenReady) { public boolean dispatchSetPlayWhenReady(Player player, boolean playWhenReady) {
player.setPlayWhenReady(playWhenReady); player.setPlayWhenReady(playWhenReady);
...@@ -36,7 +62,58 @@ public class DefaultControlDispatcher implements ControlDispatcher { ...@@ -36,7 +62,58 @@ public class DefaultControlDispatcher implements ControlDispatcher {
} }
@Override @Override
public boolean dispatchSetRepeatMode(Player player, @RepeatMode int repeatMode) { public boolean dispatchPrevious(Player player) {
Timeline timeline = player.getCurrentTimeline();
if (timeline.isEmpty() || player.isPlayingAd()) {
return true;
}
int windowIndex = player.getCurrentWindowIndex();
timeline.getWindow(windowIndex, window);
int previousWindowIndex = player.getPreviousWindowIndex();
if (previousWindowIndex != C.INDEX_UNSET
&& (player.getCurrentPosition() <= MAX_POSITION_FOR_SEEK_TO_PREVIOUS
|| (window.isDynamic && !window.isSeekable))) {
player.seekTo(previousWindowIndex, C.TIME_UNSET);
} else {
player.seekTo(windowIndex, /* positionMs= */ 0);
}
return true;
}
@Override
public boolean dispatchNext(Player player) {
Timeline timeline = player.getCurrentTimeline();
if (timeline.isEmpty() || player.isPlayingAd()) {
return true;
}
int windowIndex = player.getCurrentWindowIndex();
int nextWindowIndex = player.getNextWindowIndex();
if (nextWindowIndex != C.INDEX_UNSET) {
player.seekTo(nextWindowIndex, C.TIME_UNSET);
} else if (timeline.getWindow(windowIndex, window).isLive) {
player.seekTo(windowIndex, C.TIME_UNSET);
}
return true;
}
@Override
public boolean dispatchRewind(Player player) {
if (isRewindEnabled() && player.isCurrentWindowSeekable()) {
seekToOffset(player, -rewindIncrementMs);
}
return true;
}
@Override
public boolean dispatchFastForward(Player player) {
if (isFastForwardEnabled() && player.isCurrentWindowSeekable()) {
seekToOffset(player, fastForwardIncrementMs);
}
return true;
}
@Override
public boolean dispatchSetRepeatMode(Player player, @Player.RepeatMode int repeatMode) {
player.setRepeatMode(repeatMode); player.setRepeatMode(repeatMode);
return true; return true;
} }
...@@ -52,4 +129,44 @@ public class DefaultControlDispatcher implements ControlDispatcher { ...@@ -52,4 +129,44 @@ public class DefaultControlDispatcher implements ControlDispatcher {
player.stop(reset); player.stop(reset);
return true; return true;
} }
@Override
public boolean isRewindEnabled() {
return rewindIncrementMs > 0;
}
@Override
public boolean isFastForwardEnabled() {
return fastForwardIncrementMs > 0;
}
/**
* @deprecated Create a new instance instead and pass the new instance to the UI component. This
* makes sure the UI gets updated and is in sync with the new values.
*/
@Deprecated
public void setRewindIncrementMs(long rewindMs) {
this.rewindIncrementMs = rewindMs;
}
/**
* @deprecated Create a new instance instead and pass the new instance to the UI component. This
* makes sure the UI gets updated and is in sync with the new values.
*/
@Deprecated
public void setFastForwardIncrementMs(long fastForwardMs) {
this.fastForwardIncrementMs = fastForwardMs;
}
// Internal methods.
private static void seekToOffset(Player player, long offsetMs) {
long positionMs = player.getCurrentPosition() + offsetMs;
long durationMs = player.getDuration();
if (durationMs != C.TIME_UNSET) {
positionMs = Math.min(positionMs, durationMs);
}
positionMs = Math.max(positionMs, 0);
player.seekTo(player.getCurrentWindowIndex(), positionMs);
}
} }
...@@ -94,14 +94,14 @@ import java.util.Map; ...@@ -94,14 +94,14 @@ import java.util.Map;
* <li><b>{@code rewindIncrementMs}</b> - Sets the rewind increment. If set to zero the rewind * <li><b>{@code rewindIncrementMs}</b> - Sets the rewind increment. If set to zero the rewind
* action is not displayed. * action is not displayed.
* <ul> * <ul>
* <li>Corresponding setter: {@link #setRewindIncrementMs(long)} * <li>Corresponding setter: {@link #setControlDispatcher(ControlDispatcher)}
* <li>Default: {@link #DEFAULT_REWIND_MS} (5000) * <li>Default: {@link DefaultControlDispatcher#DEFAULT_REWIND_MS} (5000)
* </ul> * </ul>
* <li><b>{@code fastForwardIncrementMs}</b> - Sets the fast forward increment. If set to zero the * <li><b>{@code fastForwardIncrementMs}</b> - Sets the fast forward increment. If set to zero the
* fast forward action is not displayed. * fast forward action is not displayed.
* <ul> * <ul>
* <li>Corresponding setter: {@link #setFastForwardIncrementMs(long)} * <li>Corresponding setter: {@link #setControlDispatcher(ControlDispatcher)}
* <li>Default: {@link #DEFAULT_FAST_FORWARD_MS} (15000) * <li>Default: {@link DefaultControlDispatcher#DEFAULT_FAST_FORWARD_MS} (15000)
* </ul> * </ul>
* </ul> * </ul>
* *
...@@ -354,13 +354,6 @@ public class PlayerNotificationManager { ...@@ -354,13 +354,6 @@ public class PlayerNotificationManager {
}) })
public @interface Priority {} public @interface Priority {}
/** The default fast forward increment, in milliseconds. */
public static final int DEFAULT_FAST_FORWARD_MS = 15000;
/** The default rewind increment, in milliseconds. */
public static final int DEFAULT_REWIND_MS = 5000;
private static final long MAX_POSITION_FOR_SEEK_TO_PREVIOUS = 3000;
private static int instanceIdCounter; private static int instanceIdCounter;
private final Context context; private final Context context;
...@@ -392,8 +385,6 @@ public class PlayerNotificationManager { ...@@ -392,8 +385,6 @@ public class PlayerNotificationManager {
private boolean useNavigationActionsInCompactView; private boolean useNavigationActionsInCompactView;
private boolean usePlayPauseActions; private boolean usePlayPauseActions;
private boolean useStopAction; private boolean useStopAction;
private long fastForwardMs;
private long rewindMs;
private int badgeIconType; private int badgeIconType;
private boolean colorized; private boolean colorized;
private int defaults; private int defaults;
...@@ -634,8 +625,6 @@ public class PlayerNotificationManager { ...@@ -634,8 +625,6 @@ public class PlayerNotificationManager {
smallIconResourceId = R.drawable.exo_notification_small_icon; smallIconResourceId = R.drawable.exo_notification_small_icon;
defaults = 0; defaults = 0;
priority = NotificationCompat.PRIORITY_LOW; priority = NotificationCompat.PRIORITY_LOW;
fastForwardMs = DEFAULT_FAST_FORWARD_MS;
rewindMs = DEFAULT_REWIND_MS;
badgeIconType = NotificationCompat.BADGE_ICON_SMALL; badgeIconType = NotificationCompat.BADGE_ICON_SMALL;
visibility = NotificationCompat.VISIBILITY_PUBLIC; visibility = NotificationCompat.VISIBILITY_PUBLIC;
...@@ -701,12 +690,13 @@ public class PlayerNotificationManager { ...@@ -701,12 +690,13 @@ public class PlayerNotificationManager {
/** /**
* Sets the {@link ControlDispatcher}. * Sets the {@link ControlDispatcher}.
* *
* @param controlDispatcher The {@link ControlDispatcher}, or null to use {@link * @param controlDispatcher The {@link ControlDispatcher}.
* DefaultControlDispatcher}.
*/ */
public final void setControlDispatcher(ControlDispatcher controlDispatcher) { public final void setControlDispatcher(ControlDispatcher controlDispatcher) {
this.controlDispatcher = if (this.controlDispatcher != controlDispatcher) {
controlDispatcher != null ? controlDispatcher : new DefaultControlDispatcher(); this.controlDispatcher = controlDispatcher;
invalidate();
}
} }
/** /**
...@@ -725,31 +715,29 @@ public class PlayerNotificationManager { ...@@ -725,31 +715,29 @@ public class PlayerNotificationManager {
} }
/** /**
* Sets the fast forward increment in milliseconds. * @deprecated Use {@link #setControlDispatcher(ControlDispatcher)} with {@link
* * DefaultControlDispatcher#DefaultControlDispatcher(long, long)}.
* @param fastForwardMs The fast forward increment in milliseconds. A value of zero will cause the
* fast forward action to be disabled.
*/ */
@SuppressWarnings("deprecation")
@Deprecated
public final void setFastForwardIncrementMs(long fastForwardMs) { public final void setFastForwardIncrementMs(long fastForwardMs) {
if (this.fastForwardMs == fastForwardMs) { if (controlDispatcher instanceof DefaultControlDispatcher) {
return; ((DefaultControlDispatcher) controlDispatcher).setFastForwardIncrementMs(fastForwardMs);
invalidate();
} }
this.fastForwardMs = fastForwardMs;
invalidate();
} }
/** /**
* Sets the rewind increment in milliseconds. * @deprecated Use {@link #setControlDispatcher(ControlDispatcher)} with {@link
* * DefaultControlDispatcher#DefaultControlDispatcher(long, long)}.
* @param rewindMs The rewind increment in milliseconds. A value of zero will cause the rewind
* action to be disabled.
*/ */
@SuppressWarnings("deprecation")
@Deprecated
public final void setRewindIncrementMs(long rewindMs) { public final void setRewindIncrementMs(long rewindMs) {
if (this.rewindMs == rewindMs) { if (controlDispatcher instanceof DefaultControlDispatcher) {
return; ((DefaultControlDispatcher) controlDispatcher).setRewindIncrementMs(rewindMs);
invalidate();
} }
this.rewindMs = rewindMs;
invalidate();
} }
/** /**
...@@ -1047,6 +1035,7 @@ public class PlayerNotificationManager { ...@@ -1047,6 +1035,7 @@ public class PlayerNotificationManager {
List<NotificationCompat.Action> actions = new ArrayList<>(actionNames.size()); List<NotificationCompat.Action> actions = new ArrayList<>(actionNames.size());
for (int i = 0; i < actionNames.size(); i++) { for (int i = 0; i < actionNames.size(); i++) {
String actionName = actionNames.get(i); String actionName = actionNames.get(i);
@Nullable
NotificationCompat.Action action = NotificationCompat.Action action =
playbackActions.containsKey(actionName) playbackActions.containsKey(actionName)
? playbackActions.get(actionName) ? playbackActions.get(actionName)
...@@ -1146,8 +1135,8 @@ public class PlayerNotificationManager { ...@@ -1146,8 +1135,8 @@ public class PlayerNotificationManager {
if (!timeline.isEmpty() && !player.isPlayingAd()) { if (!timeline.isEmpty() && !player.isPlayingAd()) {
timeline.getWindow(player.getCurrentWindowIndex(), window); timeline.getWindow(player.getCurrentWindowIndex(), window);
enablePrevious = window.isSeekable || !window.isDynamic || player.hasPrevious(); enablePrevious = window.isSeekable || !window.isDynamic || player.hasPrevious();
enableRewind = rewindMs > 0; enableRewind = controlDispatcher.isRewindEnabled();
enableFastForward = fastForwardMs > 0; enableFastForward = controlDispatcher.isFastForwardEnabled();
enableNext = window.isDynamic || player.hasNext(); enableNext = window.isDynamic || player.hasNext();
} }
...@@ -1222,63 +1211,6 @@ public class PlayerNotificationManager { ...@@ -1222,63 +1211,6 @@ public class PlayerNotificationManager {
&& player.getPlayWhenReady(); && player.getPlayWhenReady();
} }
private void previous(Player player) {
Timeline timeline = player.getCurrentTimeline();
if (timeline.isEmpty() || player.isPlayingAd()) {
return;
}
int windowIndex = player.getCurrentWindowIndex();
timeline.getWindow(windowIndex, window);
int previousWindowIndex = player.getPreviousWindowIndex();
if (previousWindowIndex != C.INDEX_UNSET
&& (player.getCurrentPosition() <= MAX_POSITION_FOR_SEEK_TO_PREVIOUS
|| (window.isDynamic && !window.isSeekable))) {
seekTo(player, previousWindowIndex, C.TIME_UNSET);
} else {
seekTo(player, windowIndex, /* positionMs= */ 0);
}
}
private void next(Player player) {
Timeline timeline = player.getCurrentTimeline();
if (timeline.isEmpty() || player.isPlayingAd()) {
return;
}
int windowIndex = player.getCurrentWindowIndex();
int nextWindowIndex = player.getNextWindowIndex();
if (nextWindowIndex != C.INDEX_UNSET) {
seekTo(player, nextWindowIndex, C.TIME_UNSET);
} else if (timeline.getWindow(windowIndex, window).isDynamic) {
seekTo(player, windowIndex, C.TIME_UNSET);
}
}
private void rewind(Player player) {
if (player.isCurrentWindowSeekable() && rewindMs > 0) {
seekToOffset(player, /* offsetMs= */ -rewindMs);
}
}
private void fastForward(Player player) {
if (player.isCurrentWindowSeekable() && fastForwardMs > 0) {
seekToOffset(player, /* offsetMs= */ fastForwardMs);
}
}
private void seekToOffset(Player player, long offsetMs) {
long positionMs = player.getCurrentPosition() + offsetMs;
long durationMs = player.getDuration();
if (durationMs != C.TIME_UNSET) {
positionMs = Math.min(positionMs, durationMs);
}
positionMs = Math.max(positionMs, 0);
seekTo(player, player.getCurrentWindowIndex(), positionMs);
}
private void seekTo(Player player, int windowIndex, long positionMs) {
controlDispatcher.dispatchSeekTo(player, windowIndex, positionMs);
}
private boolean shouldShowPauseButton(Player player) { private boolean shouldShowPauseButton(Player player) {
return player.getPlaybackState() != Player.STATE_ENDED return player.getPlaybackState() != Player.STATE_ENDED
&& player.getPlaybackState() != Player.STATE_IDLE && player.getPlaybackState() != Player.STATE_IDLE
...@@ -1438,19 +1370,19 @@ public class PlayerNotificationManager { ...@@ -1438,19 +1370,19 @@ public class PlayerNotificationManager {
playbackPreparer.preparePlayback(); playbackPreparer.preparePlayback();
} }
} else if (player.getPlaybackState() == Player.STATE_ENDED) { } else if (player.getPlaybackState() == Player.STATE_ENDED) {
seekTo(player, player.getCurrentWindowIndex(), C.TIME_UNSET); controlDispatcher.dispatchSeekTo(player, player.getCurrentWindowIndex(), C.TIME_UNSET);
} }
controlDispatcher.dispatchSetPlayWhenReady(player, /* playWhenReady= */ true); controlDispatcher.dispatchSetPlayWhenReady(player, /* playWhenReady= */ true);
} else if (ACTION_PAUSE.equals(action)) { } else if (ACTION_PAUSE.equals(action)) {
controlDispatcher.dispatchSetPlayWhenReady(player, /* playWhenReady= */ false); controlDispatcher.dispatchSetPlayWhenReady(player, /* playWhenReady= */ false);
} else if (ACTION_PREVIOUS.equals(action)) { } else if (ACTION_PREVIOUS.equals(action)) {
previous(player); controlDispatcher.dispatchPrevious(player);
} else if (ACTION_REWIND.equals(action)) { } else if (ACTION_REWIND.equals(action)) {
rewind(player); controlDispatcher.dispatchRewind(player);
} else if (ACTION_FAST_FORWARD.equals(action)) { } else if (ACTION_FAST_FORWARD.equals(action)) {
fastForward(player); controlDispatcher.dispatchFastForward(player);
} else if (ACTION_NEXT.equals(action)) { } else if (ACTION_NEXT.equals(action)) {
next(player); controlDispatcher.dispatchNext(player);
} else if (ACTION_STOP.equals(action)) { } else if (ACTION_STOP.equals(action)) {
controlDispatcher.dispatchStop(player, /* reset= */ true); controlDispatcher.dispatchStop(player, /* reset= */ true);
} else if (ACTION_DISMISS.equals(action)) { } else if (ACTION_DISMISS.equals(action)) {
......
...@@ -965,31 +965,30 @@ public class PlayerView extends FrameLayout implements AdsLoader.AdViewProvider ...@@ -965,31 +965,30 @@ public class PlayerView extends FrameLayout implements AdsLoader.AdViewProvider
/** /**
* Sets the {@link ControlDispatcher}. * Sets the {@link ControlDispatcher}.
* *
* @param controlDispatcher The {@link ControlDispatcher}, or null to use {@link * @param controlDispatcher The {@link ControlDispatcher}.
* DefaultControlDispatcher}.
*/ */
public void setControlDispatcher(@Nullable ControlDispatcher controlDispatcher) { public void setControlDispatcher(ControlDispatcher controlDispatcher) {
Assertions.checkStateNotNull(controller); Assertions.checkStateNotNull(controller);
controller.setControlDispatcher(controlDispatcher); controller.setControlDispatcher(controlDispatcher);
} }
/** /**
* Sets the rewind increment in milliseconds. * @deprecated Use {@link #setControlDispatcher(ControlDispatcher)} with {@link
* * DefaultControlDispatcher#DefaultControlDispatcher(long, long)}.
* @param rewindMs The rewind increment in milliseconds. A non-positive value will cause the
* rewind button to be disabled.
*/ */
@SuppressWarnings("deprecation")
@Deprecated
public void setRewindIncrementMs(int rewindMs) { public void setRewindIncrementMs(int rewindMs) {
Assertions.checkStateNotNull(controller); Assertions.checkStateNotNull(controller);
controller.setRewindIncrementMs(rewindMs); controller.setRewindIncrementMs(rewindMs);
} }
/** /**
* Sets the fast forward increment in milliseconds. * @deprecated Use {@link #setControlDispatcher(ControlDispatcher)} with {@link
* * DefaultControlDispatcher#DefaultControlDispatcher(long, long)}.
* @param fastForwardMs The fast forward increment in milliseconds. A non-positive value will
* cause the fast forward button to be disabled.
*/ */
@SuppressWarnings("deprecation")
@Deprecated
public void setFastForwardIncrementMs(int fastForwardMs) { public void setFastForwardIncrementMs(int fastForwardMs) {
Assertions.checkStateNotNull(controller); Assertions.checkStateNotNull(controller);
controller.setFastForwardIncrementMs(fastForwardMs); controller.setFastForwardIncrementMs(fastForwardMs);
......
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