diff --git a/app/src/main/java/org/schabi/newpipe/player/Player.java b/app/src/main/java/org/schabi/newpipe/player/Player.java index b6098a1ef7d..6f2ba62ca9e 100644 --- a/app/src/main/java/org/schabi/newpipe/player/Player.java +++ b/app/src/main/java/org/schabi/newpipe/player/Player.java @@ -275,7 +275,7 @@ public Player(@NonNull final PlayerService service) { // notification ui in the UIs list, since the notification depends on the media session in // PlayerUi#initPlayer(), and UIs.call() guarantees UI order is preserved. UIs = new PlayerUiList( - new MediaSessionPlayerUi(this), + new MediaSessionPlayerUi(this, service.getSessionConnector()), new NotificationPlayerUi(this) ); } diff --git a/app/src/main/java/org/schabi/newpipe/player/PlayerService.java b/app/src/main/java/org/schabi/newpipe/player/PlayerService.java index d813012056e..09acdfc023a 100644 --- a/app/src/main/java/org/schabi/newpipe/player/PlayerService.java +++ b/app/src/main/java/org/schabi/newpipe/player/PlayerService.java @@ -28,10 +28,12 @@ import android.os.IBinder; import android.util.Log; +import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector; + +import org.schabi.newpipe.player.mediabrowser.MediaBrowserConnector; import org.schabi.newpipe.player.mediasession.MediaSessionPlayerUi; import org.schabi.newpipe.util.ThemeHelper; - /** * One service for all players. */ @@ -41,8 +43,8 @@ public final class PlayerService extends Service { private Player player; - private final IBinder mBinder = new PlayerService.LocalBinder(); - + private final IBinder mBinder = new LocalBinder(); + private MediaBrowserConnector mediaBrowserConnector; /*////////////////////////////////////////////////////////////////////////// // Service's LifeCycle @@ -56,7 +58,14 @@ public void onCreate() { assureCorrectAppLanguage(this); ThemeHelper.setTheme(this); - player = new Player(this); + mediaBrowserConnector = new MediaBrowserConnector(this); + } + + private void initializePlayer() { + if (player == null) { + assert mediaBrowserConnector != null; + player = new Player(this); + } } @Override @@ -73,6 +82,7 @@ public int onStartCommand(final Intent intent, final int flags, final int startI return START_NOT_STICKY; } + initializePlayer(); player.handleIntent(intent); player.UIs().get(MediaSessionPlayerUi.class) .ifPresent(ui -> ui.handleMediaButtonIntent(intent)); @@ -110,6 +120,10 @@ public void onDestroy() { Log.d(TAG, "destroy() called"); } cleanup(); + if (mediaBrowserConnector != null) { + mediaBrowserConnector.release(); + mediaBrowserConnector = null; + } } private void cleanup() { @@ -134,6 +148,10 @@ public IBinder onBind(final Intent intent) { return mBinder; } + public MediaSessionConnector getSessionConnector() { + return mediaBrowserConnector.getSessionConnector(); + } + public class LocalBinder extends Binder { public PlayerService getService() { diff --git a/app/src/main/java/org/schabi/newpipe/player/mediabrowser/MediaBrowserConnector.java b/app/src/main/java/org/schabi/newpipe/player/mediabrowser/MediaBrowserConnector.java new file mode 100644 index 00000000000..6fc61c3d748 --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/player/mediabrowser/MediaBrowserConnector.java @@ -0,0 +1,32 @@ +package org.schabi.newpipe.player.mediabrowser; + +import android.support.v4.media.session.MediaSessionCompat; + +import androidx.annotation.NonNull; + +import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector; + +import org.schabi.newpipe.player.PlayerService; + +public class MediaBrowserConnector { + private static final String TAG = MediaBrowserConnector.class.getSimpleName(); + + private final PlayerService playerService; + private final @NonNull MediaSessionConnector sessionConnector; + private final @NonNull MediaSessionCompat mediaSession; + + public MediaBrowserConnector(@NonNull final PlayerService playerService) { + this.playerService = playerService; + mediaSession = new MediaSessionCompat(playerService, TAG); + sessionConnector = new MediaSessionConnector(mediaSession); + sessionConnector.setMetadataDeduplicationEnabled(true); + } + + public @NonNull MediaSessionConnector getSessionConnector() { + return sessionConnector; + } + + public void release() { + mediaSession.release(); + } +} diff --git a/app/src/main/java/org/schabi/newpipe/player/mediasession/MediaSessionPlayerUi.java b/app/src/main/java/org/schabi/newpipe/player/mediasession/MediaSessionPlayerUi.java index e9541ab06d8..86ff6deece6 100644 --- a/app/src/main/java/org/schabi/newpipe/player/mediasession/MediaSessionPlayerUi.java +++ b/app/src/main/java/org/schabi/newpipe/player/mediasession/MediaSessionPlayerUi.java @@ -26,11 +26,14 @@ public class MediaSessionPlayerUi extends PlayerUi { private static final String TAG = "MediaSessUi"; - private MediaSessionCompat mediaSession; - private MediaSessionConnector sessionConnector; + private final @NonNull MediaSessionCompat mediaSession; + private final @NonNull MediaSessionConnector sessionConnector; - public MediaSessionPlayerUi(@NonNull final Player player) { + public MediaSessionPlayerUi(@NonNull final Player player, + @NonNull final MediaSessionConnector sessionConnector) { super(player); + this.mediaSession = sessionConnector.mediaSession; + this.sessionConnector = sessionConnector; } @Override @@ -38,39 +41,31 @@ public void initPlayer() { super.initPlayer(); destroyPlayer(); // release previously used resources - mediaSession = new MediaSessionCompat(context, TAG); mediaSession.setActive(true); - sessionConnector = new MediaSessionConnector(mediaSession); sessionConnector.setQueueNavigator(new PlayQueueNavigator(mediaSession, player)); sessionConnector.setPlayer(getForwardingPlayer()); - sessionConnector.setMetadataDeduplicationEnabled(true); sessionConnector.setMediaMetadataProvider(exoPlayer -> buildMediaMetadata()); } @Override public void destroyPlayer() { super.destroyPlayer(); - if (sessionConnector != null) { - sessionConnector.setPlayer(null); - sessionConnector.setQueueNavigator(null); - sessionConnector = null; - } - if (mediaSession != null) { - mediaSession.setActive(false); - mediaSession.release(); - mediaSession = null; - } + + sessionConnector.setPlayer(null); + sessionConnector.setQueueNavigator(null); + sessionConnector.setMediaMetadataProvider(null); + + mediaSession.setActive(false); } @Override public void onThumbnailLoaded(@Nullable final Bitmap bitmap) { super.onThumbnailLoaded(bitmap); - if (sessionConnector != null) { - // the thumbnail is now loaded: invalidate the metadata to trigger a metadata update - sessionConnector.invalidateMediaSessionMetadata(); - } + + // the thumbnail is now loaded: invalidate the metadata to trigger a metadata update + sessionConnector.invalidateMediaSessionMetadata(); } @@ -79,7 +74,7 @@ public void handleMediaButtonIntent(final Intent intent) { } public Optional getSessionToken() { - return Optional.ofNullable(mediaSession).map(MediaSessionCompat::getSessionToken); + return Optional.of(mediaSession.getSessionToken()); }