Skip to content

Commit

Permalink
Support setPlaybackSpeed(float) with the MediaSessionConnector
Browse files Browse the repository at this point in the history
Issue: #8229
#exofixit
PiperOrigin-RevId: 346968046
  • Loading branch information
marcbaechinger authored and icbaker committed Dec 14, 2020
1 parent 4ee02a2 commit 2ee4027
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 5 deletions.
4 changes: 4 additions & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@
* Notify onBufferingEnded when the state of origin player becomes
`STATE_IDLE` or `STATE_ENDED`.
* Allow to remove all playlist items that makes the player reset.
* MediaSession extension:
* Support `setPlaybackSpeed(float)` and disable it by default. Use
`MediaSessionConnector.setEnabledPlaybackActions(long)` to enable
([#8229](https:/google/ExoPlayer/issues/8229)).

### 2.12.1 (2020-10-23) ###

Expand Down
2 changes: 1 addition & 1 deletion constants.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ project.ext {
androidxAnnotationVersion = '1.1.0'
androidxAppCompatVersion = '1.1.0'
androidxCollectionVersion = '1.1.0'
androidxMediaVersion = '1.0.1'
androidxMediaVersion = '1.2.1'
androidxMultidexVersion = '2.0.0'
androidxRecyclerViewVersion = '1.1.0'
androidxTestCoreVersion = '1.3.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ public final class MediaSessionConnector {
ExoPlayerLibraryInfo.registerModule("goog.exo.mediasession");
}

/** Indicates this session supports the set playback speed command. */
// TODO(b/174297519) Replace with PlaybackStateCompat.ACTION_SET_PLAYBACK_SPEED when released.
public static final long ACTION_SET_PLAYBACK_SPEED = 1 << 22;

/** Playback actions supported by the connector. */
@LongDef(
flag = true,
Expand All @@ -113,7 +117,8 @@ public final class MediaSessionConnector {
PlaybackStateCompat.ACTION_REWIND,
PlaybackStateCompat.ACTION_STOP,
PlaybackStateCompat.ACTION_SET_REPEAT_MODE,
PlaybackStateCompat.ACTION_SET_SHUFFLE_MODE
PlaybackStateCompat.ACTION_SET_SHUFFLE_MODE,
ACTION_SET_PLAYBACK_SPEED
})
@Retention(RetentionPolicy.SOURCE)
public @interface PlaybackActions {}
Expand All @@ -128,10 +133,13 @@ public final class MediaSessionConnector {
| PlaybackStateCompat.ACTION_REWIND
| PlaybackStateCompat.ACTION_STOP
| PlaybackStateCompat.ACTION_SET_REPEAT_MODE
| PlaybackStateCompat.ACTION_SET_SHUFFLE_MODE;
| PlaybackStateCompat.ACTION_SET_SHUFFLE_MODE
| ACTION_SET_PLAYBACK_SPEED;

/** 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 - ACTION_SET_PLAYBACK_SPEED;

/**
* The name of the {@link PlaybackStateCompat} float extra with the value of {@code
Expand All @@ -145,7 +153,8 @@ public final class MediaSessionConnector {
| PlaybackStateCompat.ACTION_PAUSE
| PlaybackStateCompat.ACTION_STOP
| PlaybackStateCompat.ACTION_SET_SHUFFLE_MODE
| PlaybackStateCompat.ACTION_SET_REPEAT_MODE;
| PlaybackStateCompat.ACTION_SET_REPEAT_MODE
| ACTION_SET_PLAYBACK_SPEED;
private static final int BASE_MEDIA_SESSION_FLAGS =
MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS
| MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS;
Expand Down Expand Up @@ -1230,6 +1239,14 @@ public void onSetRepeatMode(@PlaybackStateCompat.RepeatMode int mediaSessionRepe
}
}

@Override
public void onSetPlaybackSpeed(float speed) {
if (canDispatchPlaybackAction(ACTION_SET_PLAYBACK_SPEED) && speed > 0) {
controlDispatcher.dispatchSetPlaybackParameters(
player, player.getPlaybackParameters().withSpeed(speed));
}
}

@Override
public void onSkipToNext() {
if (canDispatchToQueueNavigator(PlaybackStateCompat.ACTION_SKIP_TO_NEXT)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ public interface ControlDispatcher {
*/
boolean dispatchStop(Player player, boolean reset);

/**
* Dispatches a {@link Player#setPlaybackParameters(PlaybackParameters)} operation.
*
* @param player The {@link Player} to which the operation should be dispatched.
* @param playbackParameters The playback parameters.
* @return True if the operation was dispatched. False if suppressed.
*/
boolean dispatchSetPlaybackParameters(Player player, PlaybackParameters playbackParameters);

/** Returns {@code true} if rewind is enabled, {@code false} otherwise. */
boolean isRewindEnabled();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@ public boolean dispatchStop(Player player, boolean reset) {
return true;
}

@Override
public boolean dispatchSetPlaybackParameters(
Player player, PlaybackParameters playbackParameters) {
player.setPlaybackParameters(playbackParameters);
return true;
}

@Override
public boolean isRewindEnabled() {
return rewindIncrementMs > 0;
Expand Down

0 comments on commit 2ee4027

Please sign in to comment.