mirror of
https://github.com/TeamNewPipe/NewPipe
synced 2024-12-23 16:40:32 +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:
parent
c054ea0737
commit
bc33322d4b
@ -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 androidx.media.session.MediaButtonReceiver;
|
||||||
|
|
||||||
import com.google.android.exoplayer2.ForwardingPlayer;
|
import com.google.android.exoplayer2.ForwardingPlayer;
|
||||||
import com.google.android.exoplayer2.Player;
|
|
||||||
import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector;
|
import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector;
|
||||||
|
|
||||||
import org.schabi.newpipe.MainActivity;
|
import org.schabi.newpipe.MainActivity;
|
||||||
|
import org.schabi.newpipe.player.Player;
|
||||||
|
import org.schabi.newpipe.player.ui.VideoPlayerUi;
|
||||||
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@ -36,8 +37,7 @@ public class MediaSessionManager {
|
|||||||
private int lastAlbumArtHashCode;
|
private int lastAlbumArtHashCode;
|
||||||
|
|
||||||
public MediaSessionManager(@NonNull final Context context,
|
public MediaSessionManager(@NonNull final Context context,
|
||||||
@NonNull final Player player,
|
@NonNull final Player player) {
|
||||||
@NonNull final MediaSessionCallback callback) {
|
|
||||||
mediaSession = new MediaSessionCompat(context, TAG);
|
mediaSession = new MediaSessionCompat(context, TAG);
|
||||||
mediaSession.setActive(true);
|
mediaSession.setActive(true);
|
||||||
|
|
||||||
@ -53,16 +53,18 @@ public class MediaSessionManager {
|
|||||||
.build());
|
.build());
|
||||||
|
|
||||||
sessionConnector = new MediaSessionConnector(mediaSession);
|
sessionConnector = new MediaSessionConnector(mediaSession);
|
||||||
sessionConnector.setQueueNavigator(new PlayQueueNavigator(mediaSession, callback));
|
sessionConnector.setQueueNavigator(new PlayQueueNavigator(mediaSession, player));
|
||||||
sessionConnector.setPlayer(new ForwardingPlayer(player) {
|
sessionConnector.setPlayer(new ForwardingPlayer(player.getExoPlayer()) {
|
||||||
@Override
|
@Override
|
||||||
public void play() {
|
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
|
@Override
|
||||||
public void pause() {
|
public void pause() {
|
||||||
callback.pause();
|
player.pause();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,6 @@ import androidx.annotation.NonNull;
|
|||||||
import org.schabi.newpipe.R;
|
import org.schabi.newpipe.R;
|
||||||
import org.schabi.newpipe.extractor.stream.StreamInfo;
|
import org.schabi.newpipe.extractor.stream.StreamInfo;
|
||||||
import org.schabi.newpipe.player.Player;
|
import org.schabi.newpipe.player.Player;
|
||||||
import org.schabi.newpipe.player.playback.PlayerMediaSession;
|
|
||||||
import org.schabi.newpipe.player.ui.PlayerUi;
|
import org.schabi.newpipe.player.ui.PlayerUi;
|
||||||
import org.schabi.newpipe.util.StreamTypeUtil;
|
import org.schabi.newpipe.util.StreamTypeUtil;
|
||||||
|
|
||||||
@ -28,8 +27,7 @@ public class MediaSessionPlayerUi extends PlayerUi {
|
|||||||
if (mediaSessionManager != null) {
|
if (mediaSessionManager != null) {
|
||||||
mediaSessionManager.dispose();
|
mediaSessionManager.dispose();
|
||||||
}
|
}
|
||||||
mediaSessionManager = new MediaSessionManager(context, player.getExoPlayer(),
|
mediaSessionManager = new MediaSessionManager(context, player);
|
||||||
new PlayerMediaSession(player));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -1,106 +1,152 @@
|
|||||||
package org.schabi.newpipe.player.mediasession;
|
package org.schabi.newpipe.player.mediasession;
|
||||||
|
|
||||||
|
import android.net.Uri;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.ResultReceiver;
|
import android.os.ResultReceiver;
|
||||||
|
import android.support.v4.media.MediaDescriptionCompat;
|
||||||
|
import android.support.v4.media.MediaMetadataCompat;
|
||||||
import android.support.v4.media.session.MediaSessionCompat;
|
import android.support.v4.media.session.MediaSessionCompat;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
|
|
||||||
import com.google.android.exoplayer2.Player;
|
|
||||||
import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector;
|
import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector;
|
||||||
import com.google.android.exoplayer2.util.Util;
|
import com.google.android.exoplayer2.util.Util;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
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_NEXT;
|
||||||
import static android.support.v4.media.session.PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS;
|
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 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 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 MediaSessionCompat mediaSession;
|
||||||
private final MediaSessionCallback callback;
|
private final Player player;
|
||||||
private final int maxQueueSize;
|
|
||||||
|
|
||||||
private long activeQueueItemId;
|
private long activeQueueItemId;
|
||||||
|
|
||||||
public PlayQueueNavigator(@NonNull final MediaSessionCompat mediaSession,
|
public PlayQueueNavigator(@NonNull final MediaSessionCompat mediaSession,
|
||||||
@NonNull final MediaSessionCallback callback) {
|
@NonNull final Player player) {
|
||||||
this.mediaSession = mediaSession;
|
this.mediaSession = mediaSession;
|
||||||
this.callback = callback;
|
this.player = player;
|
||||||
this.maxQueueSize = DEFAULT_MAX_QUEUE_SIZE;
|
|
||||||
|
|
||||||
this.activeQueueItemId = MediaSessionCompat.QueueItem.UNKNOWN_ID;
|
this.activeQueueItemId = MediaSessionCompat.QueueItem.UNKNOWN_ID;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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;
|
return ACTION_SKIP_TO_NEXT | ACTION_SKIP_TO_PREVIOUS | ACTION_SKIP_TO_QUEUE_ITEM;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onTimelineChanged(@NonNull final Player player) {
|
public void onTimelineChanged(@NonNull final com.google.android.exoplayer2.Player exoPlayer) {
|
||||||
publishFloatingQueueWindow();
|
publishFloatingQueueWindow();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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
|
if (activeQueueItemId == MediaSessionCompat.QueueItem.UNKNOWN_ID
|
||||||
|| player.getCurrentTimeline().getWindowCount() > maxQueueSize) {
|
|| exoPlayer.getCurrentTimeline().getWindowCount() > MAX_QUEUE_SIZE) {
|
||||||
publishFloatingQueueWindow();
|
publishFloatingQueueWindow();
|
||||||
} else if (!player.getCurrentTimeline().isEmpty()) {
|
} else if (!exoPlayer.getCurrentTimeline().isEmpty()) {
|
||||||
activeQueueItemId = player.getCurrentMediaItemIndex();
|
activeQueueItemId = exoPlayer.getCurrentMediaItemIndex();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long getActiveQueueItemId(@Nullable final Player player) {
|
public long getActiveQueueItemId(
|
||||||
return callback.getCurrentPlayingIndex();
|
@Nullable final com.google.android.exoplayer2.Player exoPlayer) {
|
||||||
|
return Optional.ofNullable(player.getPlayQueue()).map(PlayQueue::getIndex).orElse(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSkipToPrevious(@NonNull final Player player) {
|
public void onSkipToPrevious(@NonNull final com.google.android.exoplayer2.Player exoPlayer) {
|
||||||
callback.playPrevious();
|
player.playPrevious();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSkipToQueueItem(@NonNull final Player player, final long id) {
|
public void onSkipToQueueItem(@NonNull final com.google.android.exoplayer2.Player exoPlayer,
|
||||||
callback.playItemAtIndex((int) id);
|
final long id) {
|
||||||
|
if (player.getPlayQueue() != null) {
|
||||||
|
player.selectQueueItem(player.getPlayQueue().getItem((int) id));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSkipToNext(@NonNull final Player player) {
|
public void onSkipToNext(@NonNull final com.google.android.exoplayer2.Player exoPlayer) {
|
||||||
callback.playNext();
|
player.playNext();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void publishFloatingQueueWindow() {
|
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());
|
mediaSession.setQueue(Collections.emptyList());
|
||||||
activeQueueItemId = MediaSessionCompat.QueueItem.UNKNOWN_ID;
|
activeQueueItemId = MediaSessionCompat.QueueItem.UNKNOWN_ID;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Yes this is almost a copypasta, got a problem with that? =\
|
// Yes this is almost a copypasta, got a problem with that? =\
|
||||||
final int windowCount = callback.getQueueSize();
|
final int currentWindowIndex = player.getPlayQueue().getIndex();
|
||||||
final int currentWindowIndex = callback.getCurrentPlayingIndex();
|
final int queueSize = Math.min(MAX_QUEUE_SIZE, windowCount);
|
||||||
final int queueSize = Math.min(maxQueueSize, windowCount);
|
|
||||||
final int startIndex = Util.constrainValue(currentWindowIndex - ((queueSize - 1) / 2), 0,
|
final int startIndex = Util.constrainValue(currentWindowIndex - ((queueSize - 1) / 2), 0,
|
||||||
windowCount - queueSize);
|
windowCount - queueSize);
|
||||||
|
|
||||||
final List<MediaSessionCompat.QueueItem> queue = new ArrayList<>();
|
final List<MediaSessionCompat.QueueItem> queue = new ArrayList<>();
|
||||||
for (int i = startIndex; i < startIndex + queueSize; i++) {
|
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);
|
mediaSession.setQueue(queue);
|
||||||
activeQueueItemId = currentWindowIndex;
|
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
|
@Override
|
||||||
public boolean onCommand(@NonNull final Player player,
|
public boolean onCommand(@NonNull final com.google.android.exoplayer2.Player exoPlayer,
|
||||||
@NonNull final String command,
|
@NonNull final String command,
|
||||||
@Nullable final Bundle extras,
|
@Nullable final Bundle extras,
|
||||||
@Nullable final ResultReceiver cb) {
|
@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();
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user