Skip to content

Commit

Permalink
Keep MediaSessionCompat and MediaSessionConnector in a separate class
Browse files Browse the repository at this point in the history
These objects need to live beyond the player for supporting
MediaBrowserServiceCompat and Android Auto, so they need to move outside
of the MediaSessionPlayerUi class.
  • Loading branch information
haggaie committed Aug 1, 2023
1 parent 27f7751 commit 53bf9b6
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 24 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/org/schabi/newpipe/player/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,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)
);
}
Expand Down
22 changes: 21 additions & 1 deletion app/src/main/java/org/schabi/newpipe/player/PlayerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
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;

Expand All @@ -46,6 +49,9 @@ public final class PlayerService extends Service {
private final IBinder mBinder = new PlayerService.LocalBinder(this);


private MediaBrowserConnector mediaBrowserConnector;


/*//////////////////////////////////////////////////////////////////////////
// Service's LifeCycle
//////////////////////////////////////////////////////////////////////////*/
Expand All @@ -58,7 +64,13 @@ public void onCreate() {
assureCorrectAppLanguage(this);
ThemeHelper.setTheme(this);

player = new Player(this);
mediaBrowserConnector = new MediaBrowserConnector(this);
}

private void initializePlayer() {
if (player == null) {
player = new Player(this);
}
}

@Override
Expand All @@ -75,6 +87,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));
Expand Down Expand Up @@ -112,6 +125,10 @@ public void onDestroy() {
Log.d(TAG, "destroy() called");
}
cleanup();
if (mediaBrowserConnector != null) {
mediaBrowserConnector.release();
mediaBrowserConnector = null;
}
}

private void cleanup() {
Expand All @@ -136,6 +153,9 @@ public IBinder onBind(final Intent intent) {
return mBinder;
}

public MediaSessionConnector getSessionConnector() {
return mediaBrowserConnector.getSessionConnector();
}
public static class LocalBinder extends Binder {
private final WeakReference<PlayerService> playerService;

Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,17 @@ public class MediaSessionPlayerUi extends PlayerUi
implements SharedPreferences.OnSharedPreferenceChangeListener {
private static final String TAG = "MediaSessUi";

private MediaSessionCompat mediaSession;
private MediaSessionConnector sessionConnector;
private final @NonNull MediaSessionCompat mediaSession;
private final @NonNull MediaSessionConnector sessionConnector;

private final String ignoreHardwareMediaButtonsKey;
private boolean shouldIgnoreHardwareMediaButtons = false;

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;
ignoreHardwareMediaButtonsKey =
context.getString(R.string.ignore_hardware_media_buttons_key);
}
Expand All @@ -45,10 +48,8 @@ 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());

Expand All @@ -61,34 +62,27 @@ public void initPlayer() {
updateShouldIgnoreHardwareMediaButtons(player.getPrefs());
player.getPrefs().registerOnSharedPreferenceChangeListener(this);

sessionConnector.setMetadataDeduplicationEnabled(true);
sessionConnector.setMediaMetadataProvider(exoPlayer -> buildMediaMetadata());
}

@Override
public void destroyPlayer() {
super.destroyPlayer();
player.getPrefs().unregisterOnSharedPreferenceChangeListener(this);
if (sessionConnector != null) {
sessionConnector.setMediaButtonEventHandler(null);
sessionConnector.setPlayer(null);
sessionConnector.setQueueNavigator(null);
sessionConnector = null;
}
if (mediaSession != null) {
mediaSession.setActive(false);
mediaSession.release();
mediaSession = null;
}
sessionConnector.setMediaButtonEventHandler(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();
}


Expand All @@ -111,7 +105,7 @@ public void handleMediaButtonIntent(final Intent intent) {
}

public Optional<MediaSessionCompat.Token> getSessionToken() {
return Optional.ofNullable(mediaSession).map(MediaSessionCompat::getSessionToken);
return Optional.of(mediaSession.getSessionToken());
}


Expand Down

0 comments on commit 53bf9b6

Please sign in to comment.