Support setPlaybackSpeed(float) with the MediaSessionConnector

Issue: #8229
#exofixit
PiperOrigin-RevId: 346968046
This commit is contained in:
bachinger 2020-12-11 11:33:39 +00:00 committed by Ian Baker
parent 4ee02a27de
commit 2ee40270e5
5 changed files with 42 additions and 5 deletions

View File

@ -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://github.com/google/ExoPlayer/issues/8229)).
### 2.12.1 (2020-10-23) ###

View File

@ -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'

View File

@ -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,
@ -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 {}
@ -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
@ -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;
@ -1230,6 +1239,14 @@ public final class MediaSessionConnector {
}
}
@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)) {

View File

@ -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();

View File

@ -139,6 +139,13 @@ public class DefaultControlDispatcher implements ControlDispatcher {
return true;
}
@Override
public boolean dispatchSetPlaybackParameters(
Player player, PlaybackParameters playbackParameters) {
player.setPlaybackParameters(playbackParameters);
return true;
}
@Override
public boolean isRewindEnabled() {
return rewindIncrementMs > 0;