mirror of
https://github.com/samsonjs/media.git
synced 2026-04-27 15:07:40 +00:00
Support setRepeatMode (and move shuffle action to PlaybackController)
------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=167013507
This commit is contained in:
parent
e80a93d799
commit
84c13ccbf3
4 changed files with 93 additions and 54 deletions
|
|
@ -21,6 +21,7 @@ import android.support.v4.media.session.PlaybackStateCompat;
|
||||||
|
|
||||||
import com.google.android.exoplayer2.C;
|
import com.google.android.exoplayer2.C;
|
||||||
import com.google.android.exoplayer2.Player;
|
import com.google.android.exoplayer2.Player;
|
||||||
|
import com.google.android.exoplayer2.util.RepeatModeUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A default implementation of {@link MediaSessionConnector.PlaybackController}.
|
* A default implementation of {@link MediaSessionConnector.PlaybackController}.
|
||||||
|
|
@ -40,33 +41,37 @@ public class DefaultPlaybackController implements MediaSessionConnector.Playback
|
||||||
|
|
||||||
private static final long BASE_ACTIONS = PlaybackStateCompat.ACTION_PLAY_PAUSE
|
private static final long BASE_ACTIONS = PlaybackStateCompat.ACTION_PLAY_PAUSE
|
||||||
| PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PAUSE
|
| PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PAUSE
|
||||||
| PlaybackStateCompat.ACTION_STOP;
|
| PlaybackStateCompat.ACTION_STOP | PlaybackStateCompat.ACTION_SET_SHUFFLE_MODE_ENABLED
|
||||||
|
| PlaybackStateCompat.ACTION_SET_REPEAT_MODE;
|
||||||
|
|
||||||
protected final long rewindIncrementMs;
|
protected final long rewindIncrementMs;
|
||||||
protected final long fastForwardIncrementMs;
|
protected final long fastForwardIncrementMs;
|
||||||
|
protected final int repeatToggleModes;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new instance.
|
* Creates a new instance.
|
||||||
* <p>
|
* <p>
|
||||||
* Equivalent to {@code DefaultPlaybackController(
|
* Equivalent to {@code DefaultPlaybackController(DefaultPlaybackController.DEFAULT_REWIND_MS,
|
||||||
* DefaultPlaybackController.DEFAULT_REWIND_MS,
|
* DefaultPlaybackController.DEFAULT_FAST_FORWARD_MS,
|
||||||
* DefaultPlaybackController.DEFAULT_FAST_FORWARD_MS)}.
|
* MediaSessionConnector.DEFAULT_REPEAT_TOGGLE_MODES)}.
|
||||||
*/
|
*/
|
||||||
public DefaultPlaybackController() {
|
public DefaultPlaybackController() {
|
||||||
this(DEFAULT_REWIND_MS, DEFAULT_FAST_FORWARD_MS);
|
this(DEFAULT_REWIND_MS, DEFAULT_FAST_FORWARD_MS,
|
||||||
|
MediaSessionConnector.DEFAULT_REPEAT_TOGGLE_MODES);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new instance with the given fast forward and rewind increments.
|
* Creates a new instance with the given fast forward and rewind increments.
|
||||||
*
|
* @param rewindIncrementMs The rewind increment in milliseconds. A zero or negative value will
|
||||||
* @param rewindIncrementMs The rewind increment in milliseconds. A zero or negative value will
|
|
||||||
* cause the rewind action to be disabled.
|
* cause the rewind action to be disabled.
|
||||||
* @param fastForwardIncrementMs The fast forward increment in milliseconds. A zero or negative
|
* @param fastForwardIncrementMs The fast forward increment in milliseconds. A zero or negative
|
||||||
* value will cause the fast forward action to be removed.
|
* @param repeatToggleModes The available repeatToggleModes.
|
||||||
*/
|
*/
|
||||||
public DefaultPlaybackController(long rewindIncrementMs, long fastForwardIncrementMs) {
|
public DefaultPlaybackController(long rewindIncrementMs, long fastForwardIncrementMs,
|
||||||
|
@RepeatModeUtil.RepeatToggleModes int repeatToggleModes) {
|
||||||
this.rewindIncrementMs = rewindIncrementMs;
|
this.rewindIncrementMs = rewindIncrementMs;
|
||||||
this.fastForwardIncrementMs = fastForwardIncrementMs;
|
this.fastForwardIncrementMs = fastForwardIncrementMs;
|
||||||
|
this.repeatToggleModes = repeatToggleModes;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -127,6 +132,36 @@ public class DefaultPlaybackController implements MediaSessionConnector.Playback
|
||||||
player.stop();
|
player.stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onSetShuffleMode(Player player, int shuffleMode) {
|
||||||
|
player.setShuffleModeEnabled(shuffleMode == PlaybackStateCompat.SHUFFLE_MODE_ALL
|
||||||
|
|| shuffleMode == PlaybackStateCompat.SHUFFLE_MODE_GROUP);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onSetRepeatMode(Player player, int repeatMode) {
|
||||||
|
int selectedExoPlayerRepeatMode = player.getRepeatMode();
|
||||||
|
switch (repeatMode) {
|
||||||
|
case PlaybackStateCompat.REPEAT_MODE_ALL:
|
||||||
|
case PlaybackStateCompat.REPEAT_MODE_GROUP:
|
||||||
|
if ((repeatToggleModes & RepeatModeUtil.REPEAT_TOGGLE_MODE_ALL) != 0) {
|
||||||
|
selectedExoPlayerRepeatMode = Player.REPEAT_MODE_ALL;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case PlaybackStateCompat.REPEAT_MODE_ONE:
|
||||||
|
if ((repeatToggleModes & RepeatModeUtil.REPEAT_TOGGLE_MODE_ONE) != 0) {
|
||||||
|
selectedExoPlayerRepeatMode = Player.REPEAT_MODE_ONE;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
selectedExoPlayerRepeatMode = Player.REPEAT_MODE_OFF;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
player.setRepeatMode(selectedExoPlayerRepeatMode);
|
||||||
|
}
|
||||||
|
|
||||||
|
// CommandReceiver implementation.
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String[] getCommands() {
|
public String[] getCommands() {
|
||||||
return null;
|
return null;
|
||||||
|
|
|
||||||
|
|
@ -39,6 +39,8 @@ import com.google.android.exoplayer2.Timeline;
|
||||||
import com.google.android.exoplayer2.source.TrackGroupArray;
|
import com.google.android.exoplayer2.source.TrackGroupArray;
|
||||||
import com.google.android.exoplayer2.trackselection.TrackSelectionArray;
|
import com.google.android.exoplayer2.trackselection.TrackSelectionArray;
|
||||||
import com.google.android.exoplayer2.util.ErrorMessageProvider;
|
import com.google.android.exoplayer2.util.ErrorMessageProvider;
|
||||||
|
import com.google.android.exoplayer2.util.RepeatModeUtil;
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
@ -73,6 +75,13 @@ public final class MediaSessionConnector {
|
||||||
ExoPlayerLibraryInfo.registerModule("goog.exo.mediasession");
|
ExoPlayerLibraryInfo.registerModule("goog.exo.mediasession");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The default repeat toggle modes which is the bitmask of
|
||||||
|
* {@link RepeatModeUtil#REPEAT_TOGGLE_MODE_ONE} and
|
||||||
|
* {@link RepeatModeUtil#REPEAT_TOGGLE_MODE_ALL}.
|
||||||
|
*/
|
||||||
|
public static final @RepeatModeUtil.RepeatToggleModes int DEFAULT_REPEAT_TOGGLE_MODES =
|
||||||
|
RepeatModeUtil.REPEAT_TOGGLE_MODE_ONE | RepeatModeUtil.REPEAT_TOGGLE_MODE_ALL;
|
||||||
public static final String EXTRAS_PITCH = "EXO_PITCH";
|
public static final String EXTRAS_PITCH = "EXO_PITCH";
|
||||||
private static final int BASE_MEDIA_SESSION_FLAGS = MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS
|
private static final int BASE_MEDIA_SESSION_FLAGS = MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS
|
||||||
| MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS;
|
| MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS;
|
||||||
|
|
@ -145,14 +154,17 @@ public final class MediaSessionConnector {
|
||||||
long ACTIONS = PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_PLAY
|
long ACTIONS = PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_PLAY
|
||||||
| PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_SEEK_TO
|
| PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_SEEK_TO
|
||||||
| PlaybackStateCompat.ACTION_FAST_FORWARD | PlaybackStateCompat.ACTION_REWIND
|
| PlaybackStateCompat.ACTION_FAST_FORWARD | PlaybackStateCompat.ACTION_REWIND
|
||||||
| PlaybackStateCompat.ACTION_STOP;
|
| PlaybackStateCompat.ACTION_STOP | PlaybackStateCompat.ACTION_SET_REPEAT_MODE
|
||||||
|
| PlaybackStateCompat.ACTION_SET_SHUFFLE_MODE_ENABLED;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the actions which are supported by the controller. The supported actions must be a
|
* Returns the actions which are supported by the controller. The supported actions must be a
|
||||||
* bitmask combined out of {@link PlaybackStateCompat#ACTION_PLAY_PAUSE},
|
* bitmask combined out of {@link PlaybackStateCompat#ACTION_PLAY_PAUSE},
|
||||||
* {@link PlaybackStateCompat#ACTION_PLAY}, {@link PlaybackStateCompat#ACTION_PAUSE},
|
* {@link PlaybackStateCompat#ACTION_PLAY}, {@link PlaybackStateCompat#ACTION_PAUSE},
|
||||||
* {@link PlaybackStateCompat#ACTION_SEEK_TO}, {@link PlaybackStateCompat#ACTION_FAST_FORWARD},
|
* {@link PlaybackStateCompat#ACTION_SEEK_TO}, {@link PlaybackStateCompat#ACTION_FAST_FORWARD},
|
||||||
* {@link PlaybackStateCompat#ACTION_REWIND} and {@link PlaybackStateCompat#ACTION_STOP}.
|
* {@link PlaybackStateCompat#ACTION_REWIND}, {@link PlaybackStateCompat#ACTION_STOP},
|
||||||
|
* {@link PlaybackStateCompat#ACTION_SET_REPEAT_MODE} and
|
||||||
|
* {@link PlaybackStateCompat#ACTION_SET_SHUFFLE_MODE_ENABLED}.
|
||||||
*
|
*
|
||||||
* @param player The player.
|
* @param player The player.
|
||||||
* @return The bitmask of the supported media actions.
|
* @return The bitmask of the supported media actions.
|
||||||
|
|
@ -182,6 +194,14 @@ public final class MediaSessionConnector {
|
||||||
* See {@link MediaSessionCompat.Callback#onStop()}.
|
* See {@link MediaSessionCompat.Callback#onStop()}.
|
||||||
*/
|
*/
|
||||||
void onStop(Player player);
|
void onStop(Player player);
|
||||||
|
/**
|
||||||
|
* See {@link MediaSessionCompat.Callback#onSetShuffleMode(int)}.
|
||||||
|
*/
|
||||||
|
void onSetShuffleMode(Player player, int shuffleMode);
|
||||||
|
/**
|
||||||
|
* See {@link MediaSessionCompat.Callback#onSetRepeatMode(int)}.
|
||||||
|
*/
|
||||||
|
void onSetRepeatMode(Player player, int repeatMode);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -191,15 +211,13 @@ public final class MediaSessionConnector {
|
||||||
public interface QueueNavigator extends CommandReceiver {
|
public interface QueueNavigator extends CommandReceiver {
|
||||||
|
|
||||||
long ACTIONS = PlaybackStateCompat.ACTION_SKIP_TO_QUEUE_ITEM
|
long ACTIONS = PlaybackStateCompat.ACTION_SKIP_TO_QUEUE_ITEM
|
||||||
| PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS
|
| PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS;
|
||||||
| PlaybackStateCompat.ACTION_SET_SHUFFLE_MODE_ENABLED;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the actions which are supported by the navigator. The supported actions must be a
|
* Returns the actions which are supported by the navigator. The supported actions must be a
|
||||||
* bitmask combined out of {@link PlaybackStateCompat#ACTION_SKIP_TO_QUEUE_ITEM},
|
* bitmask combined out of {@link PlaybackStateCompat#ACTION_SKIP_TO_QUEUE_ITEM},
|
||||||
* {@link PlaybackStateCompat#ACTION_SKIP_TO_NEXT},
|
* {@link PlaybackStateCompat#ACTION_SKIP_TO_NEXT},
|
||||||
* {@link PlaybackStateCompat#ACTION_SKIP_TO_PREVIOUS},
|
* {@link PlaybackStateCompat#ACTION_SKIP_TO_PREVIOUS}.
|
||||||
* {@link PlaybackStateCompat#ACTION_SET_SHUFFLE_MODE_ENABLED}.
|
|
||||||
*
|
*
|
||||||
* @param player The {@link Player}.
|
* @param player The {@link Player}.
|
||||||
* @return The bitmask of the supported media actions.
|
* @return The bitmask of the supported media actions.
|
||||||
|
|
@ -241,10 +259,6 @@ public final class MediaSessionConnector {
|
||||||
* See {@link MediaSessionCompat.Callback#onSkipToNext()}.
|
* See {@link MediaSessionCompat.Callback#onSkipToNext()}.
|
||||||
*/
|
*/
|
||||||
void onSkipToNext(Player player);
|
void onSkipToNext(Player player);
|
||||||
/**
|
|
||||||
* See {@link MediaSessionCompat.Callback#onSetShuffleMode(int)}.
|
|
||||||
*/
|
|
||||||
void onSetShuffleMode(Player player, int shuffleMode);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -429,8 +443,7 @@ public final class MediaSessionConnector {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the {@link QueueNavigator} to handle queue navigation actions {@code ACTION_SKIP_TO_NEXT},
|
* Sets the {@link QueueNavigator} to handle queue navigation actions {@code ACTION_SKIP_TO_NEXT},
|
||||||
* {@code ACTION_SKIP_TO_PREVIOUS}, {@code ACTION_SKIP_TO_QUEUE_ITEM} and
|
* {@code ACTION_SKIP_TO_PREVIOUS} and {@code ACTION_SKIP_TO_QUEUE_ITEM}.
|
||||||
* {@code ACTION_SET_SHUFFLE_MODE_ENABLED}.
|
|
||||||
*
|
*
|
||||||
* @param queueNavigator The queue navigator.
|
* @param queueNavigator The queue navigator.
|
||||||
*/
|
*/
|
||||||
|
|
@ -736,6 +749,28 @@ public final class MediaSessionConnector {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onSetShuffleModeEnabled(boolean enabled) {
|
||||||
|
if (canDispatchToPlaybackController(PlaybackStateCompat.ACTION_SET_SHUFFLE_MODE_ENABLED)) {
|
||||||
|
playbackController.onSetShuffleMode(player, enabled
|
||||||
|
? PlaybackStateCompat.SHUFFLE_MODE_ALL : PlaybackStateCompat.SHUFFLE_MODE_NONE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onSetShuffleMode(int shuffleMode) {
|
||||||
|
if (canDispatchToPlaybackController(PlaybackStateCompat.ACTION_SET_SHUFFLE_MODE_ENABLED)) {
|
||||||
|
playbackController.onSetShuffleMode(player, shuffleMode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onSetRepeatMode(int repeatMode) {
|
||||||
|
if (canDispatchToPlaybackController(PlaybackStateCompat.ACTION_SET_REPEAT_MODE)) {
|
||||||
|
playbackController.onSetRepeatMode(player, repeatMode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSkipToNext() {
|
public void onSkipToNext() {
|
||||||
if (canDispatchToQueueNavigator(PlaybackStateCompat.ACTION_SKIP_TO_NEXT)) {
|
if (canDispatchToQueueNavigator(PlaybackStateCompat.ACTION_SKIP_TO_NEXT)) {
|
||||||
|
|
@ -757,11 +792,6 @@ public final class MediaSessionConnector {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onSetRepeatMode(int repeatMode) {
|
|
||||||
// implemented as custom action
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCustomAction(@NonNull String action, @Nullable Bundle extras) {
|
public void onCustomAction(@NonNull String action, @Nullable Bundle extras) {
|
||||||
Map<String, CustomActionProvider> actionMap = customActionMap;
|
Map<String, CustomActionProvider> actionMap = customActionMap;
|
||||||
|
|
@ -842,21 +872,6 @@ public final class MediaSessionConnector {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onSetShuffleModeEnabled(boolean enabled) {
|
|
||||||
if (canDispatchToQueueNavigator(PlaybackStateCompat.ACTION_SET_SHUFFLE_MODE_ENABLED)) {
|
|
||||||
queueNavigator.onSetShuffleMode(player, enabled
|
|
||||||
? PlaybackStateCompat.SHUFFLE_MODE_ALL : PlaybackStateCompat.SHUFFLE_MODE_NONE);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onSetShuffleMode(int shuffleMode) {
|
|
||||||
if (canDispatchToQueueNavigator(PlaybackStateCompat.ACTION_SET_SHUFFLE_MODE_ENABLED)) {
|
|
||||||
queueNavigator.onSetShuffleMode(player, shuffleMode);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onAddQueueItem(MediaDescriptionCompat description) {
|
public void onAddQueueItem(MediaDescriptionCompat description) {
|
||||||
if (queueEditor != null) {
|
if (queueEditor != null) {
|
||||||
|
|
|
||||||
|
|
@ -25,12 +25,6 @@ import com.google.android.exoplayer2.util.RepeatModeUtil;
|
||||||
*/
|
*/
|
||||||
public final class RepeatModeActionProvider implements MediaSessionConnector.CustomActionProvider {
|
public final class RepeatModeActionProvider implements MediaSessionConnector.CustomActionProvider {
|
||||||
|
|
||||||
/**
|
|
||||||
* The default repeat toggle modes.
|
|
||||||
*/
|
|
||||||
public static final @RepeatModeUtil.RepeatToggleModes int DEFAULT_REPEAT_TOGGLE_MODES =
|
|
||||||
RepeatModeUtil.REPEAT_TOGGLE_MODE_ONE | RepeatModeUtil.REPEAT_TOGGLE_MODE_ALL;
|
|
||||||
|
|
||||||
private static final String ACTION_REPEAT_MODE = "ACTION_EXO_REPEAT_MODE";
|
private static final String ACTION_REPEAT_MODE = "ACTION_EXO_REPEAT_MODE";
|
||||||
|
|
||||||
private final Player player;
|
private final Player player;
|
||||||
|
|
@ -44,13 +38,13 @@ public final class RepeatModeActionProvider implements MediaSessionConnector.Cus
|
||||||
* Creates a new instance.
|
* Creates a new instance.
|
||||||
* <p>
|
* <p>
|
||||||
* Equivalent to {@code RepeatModeActionProvider(context, player,
|
* Equivalent to {@code RepeatModeActionProvider(context, player,
|
||||||
* RepeatModeActionProvider.DEFAULT_REPEAT_TOGGLE_MODES)}.
|
* MediaSessionConnector.DEFAULT_REPEAT_TOGGLE_MODES)}.
|
||||||
*
|
*
|
||||||
* @param context The context.
|
* @param context The context.
|
||||||
* @param player The player on which to toggle the repeat mode.
|
* @param player The player on which to toggle the repeat mode.
|
||||||
*/
|
*/
|
||||||
public RepeatModeActionProvider(Context context, Player player) {
|
public RepeatModeActionProvider(Context context, Player player) {
|
||||||
this(context, player, DEFAULT_REPEAT_TOGGLE_MODES);
|
this(context, player, MediaSessionConnector.DEFAULT_REPEAT_TOGGLE_MODES);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -161,11 +161,6 @@ public abstract class TimelineQueueNavigator implements MediaSessionConnector.Qu
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onSetShuffleMode(Player player, int shuffleMode) {
|
|
||||||
player.setShuffleModeEnabled(shuffleMode == PlaybackStateCompat.SHUFFLE_MODE_ALL);
|
|
||||||
}
|
|
||||||
|
|
||||||
// CommandReceiver implementation.
|
// CommandReceiver implementation.
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue