From 2ee40270e5d890ccfd6ce647ecface11137281c6 Mon Sep 17 00:00:00 2001 From: bachinger Date: Fri, 11 Dec 2020 11:33:39 +0000 Subject: [PATCH] Support setPlaybackSpeed(float) with the MediaSessionConnector Issue: #8229 #exofixit PiperOrigin-RevId: 346968046 --- RELEASENOTES.md | 4 +++ constants.gradle | 2 +- .../mediasession/MediaSessionConnector.java | 25 ++++++++++++++++--- .../android/exoplayer2/ControlDispatcher.java | 9 +++++++ .../exoplayer2/DefaultControlDispatcher.java | 7 ++++++ 5 files changed, 42 insertions(+), 5 deletions(-) diff --git a/RELEASENOTES.md b/RELEASENOTES.md index 50860c2d7a..f0846aa025 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -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) ### diff --git a/constants.gradle b/constants.gradle index 7f74b5202f..cbac079031 100644 --- a/constants.gradle +++ b/constants.gradle @@ -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' diff --git a/extensions/mediasession/src/main/java/com/google/android/exoplayer2/ext/mediasession/MediaSessionConnector.java b/extensions/mediasession/src/main/java/com/google/android/exoplayer2/ext/mediasession/MediaSessionConnector.java index 5f536643f8..179a8a3f11 100644 --- a/extensions/mediasession/src/main/java/com/google/android/exoplayer2/ext/mediasession/MediaSessionConnector.java +++ b/extensions/mediasession/src/main/java/com/google/android/exoplayer2/ext/mediasession/MediaSessionConnector.java @@ -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)) { diff --git a/library/core/src/main/java/com/google/android/exoplayer2/ControlDispatcher.java b/library/core/src/main/java/com/google/android/exoplayer2/ControlDispatcher.java index 0d5e55fc83..d3ec2cb9db 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/ControlDispatcher.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/ControlDispatcher.java @@ -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(); diff --git a/library/core/src/main/java/com/google/android/exoplayer2/DefaultControlDispatcher.java b/library/core/src/main/java/com/google/android/exoplayer2/DefaultControlDispatcher.java index 26fbdd3bcb..fe23f28db7 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/DefaultControlDispatcher.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/DefaultControlDispatcher.java @@ -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;