Skip to content

Commit

Permalink
Initialize MediaSessionCompat as part of PlayerService
Browse files Browse the repository at this point in the history
It is needed for the media browser service, so it must continue living
even if the player is closed.
  • Loading branch information
haggaie committed Jan 14, 2023
1 parent 5680939 commit a3e9df2
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 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 @@ -276,7 +276,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.getMediaSession()),
new NotificationPlayerUi(this)
);
}
Expand Down
23 changes: 20 additions & 3 deletions app/src/main/java/org/schabi/newpipe/player/PlayerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.media.MediaBrowserCompat.MediaItem;
import android.support.v4.media.session.MediaSessionCompat;
import android.util.Log;

import androidx.annotation.NonNull;
Expand All @@ -37,7 +38,6 @@
import org.schabi.newpipe.player.mediasession.MediaSessionPlayerUi;
import org.schabi.newpipe.util.ThemeHelper;

import java.util.ArrayList;
import java.util.List;

/**
Expand All @@ -49,8 +49,9 @@ public final class PlayerService extends MediaBrowserServiceCompat {

private Player player;

private final IBinder mBinder = new PlayerService.LocalBinder();
private final IBinder mBinder = new LocalBinder();
private MediaBrowserConnector mediaBrowserConnector;
private MediaSessionCompat mediaSession;


/*//////////////////////////////////////////////////////////////////////////
Expand All @@ -67,10 +68,17 @@ public void onCreate() {
assureCorrectAppLanguage(this);
ThemeHelper.setTheme(this);

player = new Player(this);
mediaSession = new MediaSessionCompat(this, TAG);
initializePlayer();
mediaBrowserConnector = new MediaBrowserConnector(this);
}

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

@Override
public int onStartCommand(final Intent intent, final int flags, final int startId) {
if (DEBUG) {
Expand All @@ -85,6 +93,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 @@ -124,6 +133,10 @@ public void onDestroy() {
Log.d(TAG, "destroy() called");
}
cleanup();
if (mediaSession != null) {
mediaSession.release();
mediaSession = null;
}
}

private void cleanup() {
Expand Down Expand Up @@ -151,6 +164,10 @@ public IBinder onBind(final Intent intent) {
return mBinder;
}

public MediaSessionCompat getMediaSession() {
return mediaSession;
}

public class LocalBinder extends Binder {

public PlayerService getService() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class MediaBrowserConnector {

public MediaBrowserConnector(@NonNull final PlayerService playerService) {
this.playerService = playerService;
playerService.setSessionToken(playerService.getMediaSession().getSessionToken());
}

@NonNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,21 @@
public class MediaSessionPlayerUi extends PlayerUi {
private static final String TAG = "MediaSessUi";

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

public MediaSessionPlayerUi(@NonNull final Player player) {
public MediaSessionPlayerUi(@NonNull final Player player,
@NonNull final MediaSessionCompat mediaSession) {
super(player);
this.mediaSession = mediaSession;
}

@Override
public void initPlayer() {
super.initPlayer();
destroyPlayer(); // release previously used resources

mediaSession = new MediaSessionCompat(context, TAG);
mediaSession.setActive(true);
player.getService().setSessionToken(mediaSession.getSessionToken());

sessionConnector = new MediaSessionConnector(mediaSession);
sessionConnector.setQueueNavigator(new PlayQueueNavigator(mediaSession, player));
Expand All @@ -58,11 +58,7 @@ public void destroyPlayer() {
sessionConnector.setQueueNavigator(null);
sessionConnector = null;
}
if (mediaSession != null) {
mediaSession.setActive(false);
mediaSession.release();
mediaSession = null;
}
mediaSession.setActive(false);
}

@Override
Expand All @@ -80,7 +76,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 a3e9df2

Please sign in to comment.