Refactor MediaSessionManager

This commit is contained in:
Stypox 2020-09-10 20:36:52 +02:00
parent bccfe500b3
commit 2017e6a3e3
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23
1 changed files with 19 additions and 17 deletions

View File

@ -16,14 +16,14 @@ import androidx.media.session.MediaButtonReceiver;
import com.google.android.exoplayer2.Player; import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector; import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector;
import org.schabi.newpipe.BuildConfig; import org.schabi.newpipe.MainActivity;
import org.schabi.newpipe.player.mediasession.MediaSessionCallback; import org.schabi.newpipe.player.mediasession.MediaSessionCallback;
import org.schabi.newpipe.player.mediasession.PlayQueueNavigator; import org.schabi.newpipe.player.mediasession.PlayQueueNavigator;
import org.schabi.newpipe.player.mediasession.PlayQueuePlaybackController; import org.schabi.newpipe.player.mediasession.PlayQueuePlaybackController;
public class MediaSessionManager { public class MediaSessionManager {
private static final String TAG = "MediaSessionManager"; private static final String TAG = MediaSessionManager.class.getSimpleName();
public static final boolean DEBUG = !BuildConfig.BUILD_TYPE.equals("release"); public static final boolean DEBUG = MainActivity.DEBUG;
@NonNull @NonNull
private final MediaSessionCompat mediaSession; private final MediaSessionCompat mediaSession;
@ -35,12 +35,12 @@ public class MediaSessionManager {
public MediaSessionManager(@NonNull final Context context, public MediaSessionManager(@NonNull final Context context,
@NonNull final Player player, @NonNull final Player player,
@NonNull final MediaSessionCallback callback) { @NonNull final MediaSessionCallback callback) {
this.mediaSession = new MediaSessionCompat(context, TAG); mediaSession = new MediaSessionCompat(context, TAG);
this.mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS
| MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS); | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
this.mediaSession.setActive(true); mediaSession.setActive(true);
this.mediaSession.setPlaybackState(new PlaybackStateCompat.Builder() mediaSession.setPlaybackState(new PlaybackStateCompat.Builder()
.setState(PlaybackStateCompat.STATE_NONE, -1, 1) .setState(PlaybackStateCompat.STATE_NONE, -1, 1)
.setActions(PlaybackStateCompat.ACTION_SEEK_TO .setActions(PlaybackStateCompat.ACTION_SEEK_TO
| PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PLAY
@ -51,10 +51,10 @@ public class MediaSessionManager {
| PlaybackStateCompat.ACTION_STOP) | PlaybackStateCompat.ACTION_STOP)
.build()); .build());
this.sessionConnector = new MediaSessionConnector(mediaSession); sessionConnector = new MediaSessionConnector(mediaSession);
this.sessionConnector.setControlDispatcher(new PlayQueuePlaybackController(callback)); sessionConnector.setControlDispatcher(new PlayQueuePlaybackController(callback));
this.sessionConnector.setQueueNavigator(new PlayQueueNavigator(mediaSession, callback)); sessionConnector.setQueueNavigator(new PlayQueueNavigator(mediaSession, callback));
this.sessionConnector.setPlayer(player); sessionConnector.setPlayer(player);
} }
@Nullable @Nullable
@ -64,10 +64,12 @@ public class MediaSessionManager {
} }
public MediaSessionCompat.Token getSessionToken() { public MediaSessionCompat.Token getSessionToken() {
return this.mediaSession.getSessionToken(); return mediaSession.getSessionToken();
} }
public void setMetadata(final String title, final String artist, final Bitmap albumArt, public void setMetadata(final String title,
final String artist,
final Bitmap albumArt,
final long duration) { final long duration) {
if (albumArt == null || !mediaSession.isActive()) { if (albumArt == null || !mediaSession.isActive()) {
return; return;
@ -130,9 +132,9 @@ public class MediaSessionManager {
* Should be called on player destruction to prevent leakage. * Should be called on player destruction to prevent leakage.
*/ */
public void dispose() { public void dispose() {
this.sessionConnector.setPlayer(null); sessionConnector.setPlayer(null);
this.sessionConnector.setQueueNavigator(null); sessionConnector.setQueueNavigator(null);
this.mediaSession.setActive(false); mediaSession.setActive(false);
this.mediaSession.release(); mediaSession.release();
} }
} }