mirror of
https://github.com/TeamNewPipe/NewPipe
synced 2025-01-08 16:30:34 +00:00
Keep MediaSessionCompat and MediaSessionConnector in a separate class
These objects need to live beyond the player for supporting MediaBrowserServiceCompat and Android Auto, so they need to move outside of the MediaSessionPlayerUi class.
This commit is contained in:
parent
70748fa0bc
commit
c73763d2c3
@ -302,7 +302,7 @@ public final class Player implements PlaybackListener, Listener {
|
|||||||
// notification ui in the UIs list, since the notification depends on the media session in
|
// 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.
|
// PlayerUi#initPlayer(), and UIs.call() guarantees UI order is preserved.
|
||||||
UIs = new PlayerUiList(
|
UIs = new PlayerUiList(
|
||||||
new MediaSessionPlayerUi(this),
|
new MediaSessionPlayerUi(this, service.getSessionConnector()),
|
||||||
new NotificationPlayerUi(this)
|
new NotificationPlayerUi(this)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -28,6 +28,9 @@ import android.os.Binder;
|
|||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
import android.util.Log;
|
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.player.mediasession.MediaSessionPlayerUi;
|
||||||
import org.schabi.newpipe.player.notification.NotificationPlayerUi;
|
import org.schabi.newpipe.player.notification.NotificationPlayerUi;
|
||||||
import org.schabi.newpipe.util.ThemeHelper;
|
import org.schabi.newpipe.util.ThemeHelper;
|
||||||
@ -47,6 +50,9 @@ public final class PlayerService extends Service {
|
|||||||
private final IBinder mBinder = new PlayerService.LocalBinder(this);
|
private final IBinder mBinder = new PlayerService.LocalBinder(this);
|
||||||
|
|
||||||
|
|
||||||
|
private MediaBrowserConnector mediaBrowserConnector;
|
||||||
|
|
||||||
|
|
||||||
/*//////////////////////////////////////////////////////////////////////////
|
/*//////////////////////////////////////////////////////////////////////////
|
||||||
// Service's LifeCycle
|
// Service's LifeCycle
|
||||||
//////////////////////////////////////////////////////////////////////////*/
|
//////////////////////////////////////////////////////////////////////////*/
|
||||||
@ -59,15 +65,21 @@ public final class PlayerService extends Service {
|
|||||||
assureCorrectAppLanguage(this);
|
assureCorrectAppLanguage(this);
|
||||||
ThemeHelper.setTheme(this);
|
ThemeHelper.setTheme(this);
|
||||||
|
|
||||||
player = new Player(this);
|
mediaBrowserConnector = new MediaBrowserConnector(this);
|
||||||
/*
|
}
|
||||||
Create the player notification and start immediately the service in foreground,
|
|
||||||
otherwise if nothing is played or initializing the player and its components (especially
|
private void initializePlayer() {
|
||||||
loading stream metadata) takes a lot of time, the app would crash on Android 8+ as the
|
if (player == null) {
|
||||||
service would never be put in the foreground while we said to the system we would do so
|
player = new Player(this);
|
||||||
*/
|
/*
|
||||||
player.UIs().get(NotificationPlayerUi.class)
|
Create the player notification and start immediately the service in foreground,
|
||||||
.ifPresent(NotificationPlayerUi::createNotificationAndStartForeground);
|
otherwise if nothing is played or initializing the player and its components (especially
|
||||||
|
loading stream metadata) takes a lot of time, the app would crash on Android 8+ as the
|
||||||
|
service would never be put in the foreground while we said to the system we would do so
|
||||||
|
*/
|
||||||
|
player.UIs().get(NotificationPlayerUi.class)
|
||||||
|
.ifPresent(NotificationPlayerUi::createNotificationAndStartForeground);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -104,11 +116,10 @@ public final class PlayerService extends Service {
|
|||||||
return START_NOT_STICKY;
|
return START_NOT_STICKY;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (player != null) {
|
initializePlayer();
|
||||||
player.handleIntent(intent);
|
player.handleIntent(intent);
|
||||||
player.UIs().get(MediaSessionPlayerUi.class)
|
player.UIs().get(MediaSessionPlayerUi.class)
|
||||||
.ifPresent(ui -> ui.handleMediaButtonIntent(intent));
|
.ifPresent(ui -> ui.handleMediaButtonIntent(intent));
|
||||||
}
|
|
||||||
|
|
||||||
return START_NOT_STICKY;
|
return START_NOT_STICKY;
|
||||||
}
|
}
|
||||||
@ -143,6 +154,10 @@ public final class PlayerService extends Service {
|
|||||||
Log.d(TAG, "destroy() called");
|
Log.d(TAG, "destroy() called");
|
||||||
}
|
}
|
||||||
cleanup();
|
cleanup();
|
||||||
|
if (mediaBrowserConnector != null) {
|
||||||
|
mediaBrowserConnector.release();
|
||||||
|
mediaBrowserConnector = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void cleanup() {
|
private void cleanup() {
|
||||||
@ -167,6 +182,9 @@ public final class PlayerService extends Service {
|
|||||||
return mBinder;
|
return mBinder;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public MediaSessionConnector getSessionConnector() {
|
||||||
|
return mediaBrowserConnector.getSessionConnector();
|
||||||
|
}
|
||||||
public static class LocalBinder extends Binder {
|
public static class LocalBinder extends Binder {
|
||||||
private final WeakReference<PlayerService> playerService;
|
private final WeakReference<PlayerService> playerService;
|
||||||
|
|
||||||
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
@ -50,8 +50,11 @@ public class MediaSessionPlayerUi extends PlayerUi
|
|||||||
private List<NotificationActionData> prevNotificationActions = List.of();
|
private List<NotificationActionData> prevNotificationActions = List.of();
|
||||||
|
|
||||||
|
|
||||||
public MediaSessionPlayerUi(@NonNull final Player player) {
|
public MediaSessionPlayerUi(@NonNull final Player player,
|
||||||
|
@NonNull final MediaSessionConnector sessionConnector) {
|
||||||
super(player);
|
super(player);
|
||||||
|
this.mediaSession = sessionConnector.mediaSession;
|
||||||
|
this.sessionConnector = sessionConnector;
|
||||||
ignoreHardwareMediaButtonsKey =
|
ignoreHardwareMediaButtonsKey =
|
||||||
context.getString(R.string.ignore_hardware_media_buttons_key);
|
context.getString(R.string.ignore_hardware_media_buttons_key);
|
||||||
}
|
}
|
||||||
@ -61,10 +64,8 @@ public class MediaSessionPlayerUi extends PlayerUi
|
|||||||
super.initPlayer();
|
super.initPlayer();
|
||||||
destroyPlayer(); // release previously used resources
|
destroyPlayer(); // release previously used resources
|
||||||
|
|
||||||
mediaSession = new MediaSessionCompat(context, TAG);
|
|
||||||
mediaSession.setActive(true);
|
mediaSession.setActive(true);
|
||||||
|
|
||||||
sessionConnector = new MediaSessionConnector(mediaSession);
|
|
||||||
sessionConnector.setQueueNavigator(new PlayQueueNavigator(mediaSession, player));
|
sessionConnector.setQueueNavigator(new PlayQueueNavigator(mediaSession, player));
|
||||||
sessionConnector.setPlayer(getForwardingPlayer());
|
sessionConnector.setPlayer(getForwardingPlayer());
|
||||||
|
|
||||||
@ -77,7 +78,6 @@ public class MediaSessionPlayerUi extends PlayerUi
|
|||||||
updateShouldIgnoreHardwareMediaButtons(player.getPrefs());
|
updateShouldIgnoreHardwareMediaButtons(player.getPrefs());
|
||||||
player.getPrefs().registerOnSharedPreferenceChangeListener(this);
|
player.getPrefs().registerOnSharedPreferenceChangeListener(this);
|
||||||
|
|
||||||
sessionConnector.setMetadataDeduplicationEnabled(true);
|
|
||||||
sessionConnector.setMediaMetadataProvider(exoPlayer -> buildMediaMetadata());
|
sessionConnector.setMediaMetadataProvider(exoPlayer -> buildMediaMetadata());
|
||||||
|
|
||||||
// force updating media session actions by resetting the previous ones
|
// force updating media session actions by resetting the previous ones
|
||||||
@ -89,27 +89,23 @@ public class MediaSessionPlayerUi extends PlayerUi
|
|||||||
public void destroyPlayer() {
|
public void destroyPlayer() {
|
||||||
super.destroyPlayer();
|
super.destroyPlayer();
|
||||||
player.getPrefs().unregisterOnSharedPreferenceChangeListener(this);
|
player.getPrefs().unregisterOnSharedPreferenceChangeListener(this);
|
||||||
if (sessionConnector != null) {
|
|
||||||
sessionConnector.setMediaButtonEventHandler(null);
|
sessionConnector.setMediaButtonEventHandler(null);
|
||||||
sessionConnector.setPlayer(null);
|
sessionConnector.setPlayer(null);
|
||||||
sessionConnector.setQueueNavigator(null);
|
sessionConnector.setQueueNavigator(null);
|
||||||
sessionConnector = null;
|
sessionConnector.setMediaMetadataProvider(null);
|
||||||
}
|
|
||||||
if (mediaSession != null) {
|
mediaSession.setActive(false);
|
||||||
mediaSession.setActive(false);
|
|
||||||
mediaSession.release();
|
|
||||||
mediaSession = null;
|
|
||||||
}
|
|
||||||
prevNotificationActions = List.of();
|
prevNotificationActions = List.of();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onThumbnailLoaded(@Nullable final Bitmap bitmap) {
|
public void onThumbnailLoaded(@Nullable final Bitmap bitmap) {
|
||||||
super.onThumbnailLoaded(bitmap);
|
super.onThumbnailLoaded(bitmap);
|
||||||
if (sessionConnector != null) {
|
|
||||||
// the thumbnail is now loaded: invalidate the metadata to trigger a metadata update
|
// the thumbnail is now loaded: invalidate the metadata to trigger a metadata update
|
||||||
sessionConnector.invalidateMediaSessionMetadata();
|
sessionConnector.invalidateMediaSessionMetadata();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -132,7 +128,7 @@ public class MediaSessionPlayerUi extends PlayerUi
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Optional<MediaSessionCompat.Token> getSessionToken() {
|
public Optional<MediaSessionCompat.Token> getSessionToken() {
|
||||||
return Optional.ofNullable(mediaSession).map(MediaSessionCompat::getSessionToken);
|
return Optional.of(mediaSession.getSessionToken());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user