mirror of
				https://github.com/TeamNewPipe/NewPipe
				synced 2025-10-31 07:13:00 +00:00 
			
		
		
		
	Remove useless MediaSessionCallback
The player is now passed directly, it made no sense to wrap around it in a callback that was not really a callback but rather, actually, a wrapper.
This commit is contained in:
		| @@ -1,21 +0,0 @@ | ||||
| package org.schabi.newpipe.player.mediasession; | ||||
|  | ||||
| import android.support.v4.media.MediaDescriptionCompat; | ||||
|  | ||||
| public interface MediaSessionCallback { | ||||
|     void playPrevious(); | ||||
|  | ||||
|     void playNext(); | ||||
|  | ||||
|     void playItemAtIndex(int index); | ||||
|  | ||||
|     int getCurrentPlayingIndex(); | ||||
|  | ||||
|     int getQueueSize(); | ||||
|  | ||||
|     MediaDescriptionCompat getQueueMetadata(int index); | ||||
|  | ||||
|     void play(); | ||||
|  | ||||
|     void pause(); | ||||
| } | ||||
| @@ -14,10 +14,11 @@ import androidx.annotation.Nullable; | ||||
| import androidx.media.session.MediaButtonReceiver; | ||||
|  | ||||
| import com.google.android.exoplayer2.ForwardingPlayer; | ||||
| import com.google.android.exoplayer2.Player; | ||||
| import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector; | ||||
|  | ||||
| import org.schabi.newpipe.MainActivity; | ||||
| import org.schabi.newpipe.player.Player; | ||||
| import org.schabi.newpipe.player.ui.VideoPlayerUi; | ||||
|  | ||||
| import java.util.Optional; | ||||
|  | ||||
| @@ -36,8 +37,7 @@ public class MediaSessionManager { | ||||
|     private int lastAlbumArtHashCode; | ||||
|  | ||||
|     public MediaSessionManager(@NonNull final Context context, | ||||
|                                @NonNull final Player player, | ||||
|                                @NonNull final MediaSessionCallback callback) { | ||||
|                                @NonNull final Player player) { | ||||
|         mediaSession = new MediaSessionCompat(context, TAG); | ||||
|         mediaSession.setActive(true); | ||||
|  | ||||
| @@ -53,16 +53,18 @@ public class MediaSessionManager { | ||||
|                 .build()); | ||||
|  | ||||
|         sessionConnector = new MediaSessionConnector(mediaSession); | ||||
|         sessionConnector.setQueueNavigator(new PlayQueueNavigator(mediaSession, callback)); | ||||
|         sessionConnector.setPlayer(new ForwardingPlayer(player) { | ||||
|         sessionConnector.setQueueNavigator(new PlayQueueNavigator(mediaSession, player)); | ||||
|         sessionConnector.setPlayer(new ForwardingPlayer(player.getExoPlayer()) { | ||||
|             @Override | ||||
|             public void play() { | ||||
|                 callback.play(); | ||||
|                 player.play(); | ||||
|                 // hide the player controls even if the play command came from the media session | ||||
|                 player.UIs().get(VideoPlayerUi.class).ifPresent(ui -> ui.hideControls(0, 0)); | ||||
|             } | ||||
|  | ||||
|             @Override | ||||
|             public void pause() { | ||||
|                 callback.pause(); | ||||
|                 player.pause(); | ||||
|             } | ||||
|         }); | ||||
|     } | ||||
|   | ||||
| @@ -8,7 +8,6 @@ import androidx.annotation.NonNull; | ||||
| import org.schabi.newpipe.R; | ||||
| import org.schabi.newpipe.extractor.stream.StreamInfo; | ||||
| import org.schabi.newpipe.player.Player; | ||||
| import org.schabi.newpipe.player.playback.PlayerMediaSession; | ||||
| import org.schabi.newpipe.player.ui.PlayerUi; | ||||
| import org.schabi.newpipe.util.StreamTypeUtil; | ||||
|  | ||||
| @@ -28,8 +27,7 @@ public class MediaSessionPlayerUi extends PlayerUi { | ||||
|         if (mediaSessionManager != null) { | ||||
|             mediaSessionManager.dispose(); | ||||
|         } | ||||
|         mediaSessionManager = new MediaSessionManager(context, player.getExoPlayer(), | ||||
|                 new PlayerMediaSession(player)); | ||||
|         mediaSessionManager = new MediaSessionManager(context, player); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|   | ||||
| @@ -1,106 +1,152 @@ | ||||
| package org.schabi.newpipe.player.mediasession; | ||||
|  | ||||
| import android.net.Uri; | ||||
| import android.os.Bundle; | ||||
| import android.os.ResultReceiver; | ||||
| import android.support.v4.media.MediaDescriptionCompat; | ||||
| import android.support.v4.media.MediaMetadataCompat; | ||||
| import android.support.v4.media.session.MediaSessionCompat; | ||||
|  | ||||
| import androidx.annotation.NonNull; | ||||
| import androidx.annotation.Nullable; | ||||
|  | ||||
| import com.google.android.exoplayer2.Player; | ||||
| import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector; | ||||
| import com.google.android.exoplayer2.util.Util; | ||||
|  | ||||
| import java.util.ArrayList; | ||||
| import java.util.Collections; | ||||
| import java.util.List; | ||||
| import java.util.Optional; | ||||
|  | ||||
| import static android.support.v4.media.session.PlaybackStateCompat.ACTION_SKIP_TO_NEXT; | ||||
| import static android.support.v4.media.session.PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS; | ||||
| import static android.support.v4.media.session.PlaybackStateCompat.ACTION_SKIP_TO_QUEUE_ITEM; | ||||
|  | ||||
| import org.schabi.newpipe.player.Player; | ||||
| import org.schabi.newpipe.player.playqueue.PlayQueue; | ||||
| import org.schabi.newpipe.player.playqueue.PlayQueueItem; | ||||
|  | ||||
| public class PlayQueueNavigator implements MediaSessionConnector.QueueNavigator { | ||||
|     public static final int DEFAULT_MAX_QUEUE_SIZE = 10; | ||||
|     private static final int MAX_QUEUE_SIZE = 10; | ||||
|  | ||||
|     private final MediaSessionCompat mediaSession; | ||||
|     private final MediaSessionCallback callback; | ||||
|     private final int maxQueueSize; | ||||
|     private final Player player; | ||||
|  | ||||
|     private long activeQueueItemId; | ||||
|  | ||||
|     public PlayQueueNavigator(@NonNull final MediaSessionCompat mediaSession, | ||||
|                               @NonNull final MediaSessionCallback callback) { | ||||
|                               @NonNull final Player player) { | ||||
|         this.mediaSession = mediaSession; | ||||
|         this.callback = callback; | ||||
|         this.maxQueueSize = DEFAULT_MAX_QUEUE_SIZE; | ||||
|         this.player = player; | ||||
|  | ||||
|         this.activeQueueItemId = MediaSessionCompat.QueueItem.UNKNOWN_ID; | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public long getSupportedQueueNavigatorActions(@Nullable final Player player) { | ||||
|     public long getSupportedQueueNavigatorActions( | ||||
|             @Nullable final com.google.android.exoplayer2.Player exoPlayer) { | ||||
|         return ACTION_SKIP_TO_NEXT | ACTION_SKIP_TO_PREVIOUS | ACTION_SKIP_TO_QUEUE_ITEM; | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void onTimelineChanged(@NonNull final Player player) { | ||||
|     public void onTimelineChanged(@NonNull final com.google.android.exoplayer2.Player exoPlayer) { | ||||
|         publishFloatingQueueWindow(); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void onCurrentMediaItemIndexChanged(@NonNull final Player player) { | ||||
|     public void onCurrentMediaItemIndexChanged( | ||||
|             @NonNull final com.google.android.exoplayer2.Player exoPlayer) { | ||||
|         if (activeQueueItemId == MediaSessionCompat.QueueItem.UNKNOWN_ID | ||||
|                 || player.getCurrentTimeline().getWindowCount() > maxQueueSize) { | ||||
|                 || exoPlayer.getCurrentTimeline().getWindowCount() > MAX_QUEUE_SIZE) { | ||||
|             publishFloatingQueueWindow(); | ||||
|         } else if (!player.getCurrentTimeline().isEmpty()) { | ||||
|             activeQueueItemId = player.getCurrentMediaItemIndex(); | ||||
|         } else if (!exoPlayer.getCurrentTimeline().isEmpty()) { | ||||
|             activeQueueItemId = exoPlayer.getCurrentMediaItemIndex(); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public long getActiveQueueItemId(@Nullable final Player player) { | ||||
|         return callback.getCurrentPlayingIndex(); | ||||
|     public long getActiveQueueItemId( | ||||
|             @Nullable final com.google.android.exoplayer2.Player exoPlayer) { | ||||
|         return Optional.ofNullable(player.getPlayQueue()).map(PlayQueue::getIndex).orElse(-1); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void onSkipToPrevious(@NonNull final Player player) { | ||||
|         callback.playPrevious(); | ||||
|     public void onSkipToPrevious(@NonNull final com.google.android.exoplayer2.Player exoPlayer) { | ||||
|         player.playPrevious(); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void onSkipToQueueItem(@NonNull final Player player, final long id) { | ||||
|         callback.playItemAtIndex((int) id); | ||||
|     public void onSkipToQueueItem(@NonNull final com.google.android.exoplayer2.Player exoPlayer, | ||||
|                                   final long id) { | ||||
|         if (player.getPlayQueue() != null) { | ||||
|             player.selectQueueItem(player.getPlayQueue().getItem((int) id)); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void onSkipToNext(@NonNull final Player player) { | ||||
|         callback.playNext(); | ||||
|     public void onSkipToNext(@NonNull final com.google.android.exoplayer2.Player exoPlayer) { | ||||
|         player.playNext(); | ||||
|     } | ||||
|  | ||||
|     private void publishFloatingQueueWindow() { | ||||
|         if (callback.getQueueSize() == 0) { | ||||
|         final int windowCount = Optional.ofNullable(player.getPlayQueue()) | ||||
|                 .map(PlayQueue::size) | ||||
|                 .orElse(0); | ||||
|         if (windowCount == 0) { | ||||
|             mediaSession.setQueue(Collections.emptyList()); | ||||
|             activeQueueItemId = MediaSessionCompat.QueueItem.UNKNOWN_ID; | ||||
|             return; | ||||
|         } | ||||
|  | ||||
|         // Yes this is almost a copypasta, got a problem with that? =\ | ||||
|         final int windowCount = callback.getQueueSize(); | ||||
|         final int currentWindowIndex = callback.getCurrentPlayingIndex(); | ||||
|         final int queueSize = Math.min(maxQueueSize, windowCount); | ||||
|         final int currentWindowIndex = player.getPlayQueue().getIndex(); | ||||
|         final int queueSize = Math.min(MAX_QUEUE_SIZE, windowCount); | ||||
|         final int startIndex = Util.constrainValue(currentWindowIndex - ((queueSize - 1) / 2), 0, | ||||
|                 windowCount - queueSize); | ||||
|  | ||||
|         final List<MediaSessionCompat.QueueItem> queue = new ArrayList<>(); | ||||
|         for (int i = startIndex; i < startIndex + queueSize; i++) { | ||||
|             queue.add(new MediaSessionCompat.QueueItem(callback.getQueueMetadata(i), i)); | ||||
|             queue.add(new MediaSessionCompat.QueueItem(getQueueMetadata(i), i)); | ||||
|         } | ||||
|         mediaSession.setQueue(queue); | ||||
|         activeQueueItemId = currentWindowIndex; | ||||
|     } | ||||
|  | ||||
|     public MediaDescriptionCompat getQueueMetadata(final int index) { | ||||
|         if (player.getPlayQueue() == null) { | ||||
|             return null; | ||||
|         } | ||||
|         final PlayQueueItem item = player.getPlayQueue().getItem(index); | ||||
|         if (item == null) { | ||||
|             return null; | ||||
|         } | ||||
|  | ||||
|         final MediaDescriptionCompat.Builder descBuilder = new MediaDescriptionCompat.Builder() | ||||
|                 .setMediaId(String.valueOf(index)) | ||||
|                 .setTitle(item.getTitle()) | ||||
|                 .setSubtitle(item.getUploader()); | ||||
|  | ||||
|         // set additional metadata for A2DP/AVRCP | ||||
|         final Bundle additionalMetadata = new Bundle(); | ||||
|         additionalMetadata.putString(MediaMetadataCompat.METADATA_KEY_TITLE, item.getTitle()); | ||||
|         additionalMetadata.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, item.getUploader()); | ||||
|         additionalMetadata | ||||
|                 .putLong(MediaMetadataCompat.METADATA_KEY_DURATION, item.getDuration() * 1000); | ||||
|         additionalMetadata.putLong(MediaMetadataCompat.METADATA_KEY_TRACK_NUMBER, index + 1); | ||||
|         additionalMetadata | ||||
|                 .putLong(MediaMetadataCompat.METADATA_KEY_NUM_TRACKS, player.getPlayQueue().size()); | ||||
|         descBuilder.setExtras(additionalMetadata); | ||||
|  | ||||
|         final Uri thumbnailUri = Uri.parse(item.getThumbnailUrl()); | ||||
|         if (thumbnailUri != null) { | ||||
|             descBuilder.setIconUri(thumbnailUri); | ||||
|         } | ||||
|  | ||||
|         return descBuilder.build(); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public boolean onCommand(@NonNull final Player player, | ||||
|     public boolean onCommand(@NonNull final com.google.android.exoplayer2.Player exoPlayer, | ||||
|                              @NonNull final String command, | ||||
|                              @Nullable final Bundle extras, | ||||
|                              @Nullable final ResultReceiver cb) { | ||||
|   | ||||
| @@ -1,99 +0,0 @@ | ||||
| package org.schabi.newpipe.player.playback; | ||||
|  | ||||
| import android.net.Uri; | ||||
| import android.os.Bundle; | ||||
| import android.support.v4.media.MediaDescriptionCompat; | ||||
| import android.support.v4.media.MediaMetadataCompat; | ||||
|  | ||||
| import org.schabi.newpipe.player.Player; | ||||
| import org.schabi.newpipe.player.mediasession.MediaSessionCallback; | ||||
| import org.schabi.newpipe.player.playqueue.PlayQueueItem; | ||||
| import org.schabi.newpipe.player.ui.VideoPlayerUi; | ||||
|  | ||||
| public class PlayerMediaSession implements MediaSessionCallback { | ||||
|     private final Player player; | ||||
|  | ||||
|     public PlayerMediaSession(final Player player) { | ||||
|         this.player = player; | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void playPrevious() { | ||||
|         player.playPrevious(); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void playNext() { | ||||
|         player.playNext(); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void playItemAtIndex(final int index) { | ||||
|         if (player.getPlayQueue() == null) { | ||||
|             return; | ||||
|         } | ||||
|         player.selectQueueItem(player.getPlayQueue().getItem(index)); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public int getCurrentPlayingIndex() { | ||||
|         if (player.getPlayQueue() == null) { | ||||
|             return -1; | ||||
|         } | ||||
|         return player.getPlayQueue().getIndex(); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public int getQueueSize() { | ||||
|         if (player.getPlayQueue() == null) { | ||||
|             return -1; | ||||
|         } | ||||
|         return player.getPlayQueue().size(); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public MediaDescriptionCompat getQueueMetadata(final int index) { | ||||
|         if (player.getPlayQueue() == null) { | ||||
|             return null; | ||||
|         } | ||||
|         final PlayQueueItem item = player.getPlayQueue().getItem(index); | ||||
|         if (item == null) { | ||||
|             return null; | ||||
|         } | ||||
|  | ||||
|         final MediaDescriptionCompat.Builder descBuilder = new MediaDescriptionCompat.Builder() | ||||
|                 .setMediaId(String.valueOf(index)) | ||||
|                 .setTitle(item.getTitle()) | ||||
|                 .setSubtitle(item.getUploader()); | ||||
|  | ||||
|         // set additional metadata for A2DP/AVRCP | ||||
|         final Bundle additionalMetadata = new Bundle(); | ||||
|         additionalMetadata.putString(MediaMetadataCompat.METADATA_KEY_TITLE, item.getTitle()); | ||||
|         additionalMetadata.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, item.getUploader()); | ||||
|         additionalMetadata | ||||
|                 .putLong(MediaMetadataCompat.METADATA_KEY_DURATION, item.getDuration() * 1000); | ||||
|         additionalMetadata.putLong(MediaMetadataCompat.METADATA_KEY_TRACK_NUMBER, index + 1); | ||||
|         additionalMetadata | ||||
|                 .putLong(MediaMetadataCompat.METADATA_KEY_NUM_TRACKS, player.getPlayQueue().size()); | ||||
|         descBuilder.setExtras(additionalMetadata); | ||||
|  | ||||
|         final Uri thumbnailUri = Uri.parse(item.getThumbnailUrl()); | ||||
|         if (thumbnailUri != null) { | ||||
|             descBuilder.setIconUri(thumbnailUri); | ||||
|         } | ||||
|  | ||||
|         return descBuilder.build(); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void play() { | ||||
|         player.play(); | ||||
|         // hide the player controls even if the play command came from the media session | ||||
|         player.UIs().get(VideoPlayerUi.class).ifPresent(playerUi -> playerUi.hideControls(0, 0)); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void pause() { | ||||
|         player.pause(); | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user
	 Stypox
					Stypox