private void setUpMediaSession() { mSession = new MediaSessionCompat(this, "Timber"); mSession.setCallback(new MediaSessionCompat.Callback() { @Override public void onPause() { mSession.setFlags(MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS | MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS);
@Nullable @SuppressWarnings("UnusedReturnValue") public KeyEvent handleMediaButtonIntent(final Intent intent) { return MediaButtonReceiver.handleIntent(mediaSession, intent); }
@Override public void onRepeatModeChanged(@Player.RepeatMode int repeatMode) { mediaSession.setRepeatMode( repeatMode == Player.REPEAT_MODE_ONE ? PlaybackStateCompat.REPEAT_MODE_ONE : repeatMode == Player.REPEAT_MODE_ALL ? PlaybackStateCompat.REPEAT_MODE_ALL : PlaybackStateCompat.REPEAT_MODE_NONE); invalidateMediaSessionPlaybackState(); }
public MediaSessionManager(@NonNull final Context context, @NonNull final Player player, @NonNull final MediaSessionCallback callback) { this.mediaSession = new MediaSessionCompat(context, TAG); this.mediaSession.setActive(true); this.sessionConnector = new MediaSessionConnector(mediaSession, new PlayQueuePlaybackController(callback)); this.sessionConnector.setQueueNavigator(new PlayQueueNavigator(mediaSession, callback)); this.sessionConnector.setPlayer(player, null); }
/** * Updates the metadata of the media session. * * <p>Apps normally only need to call this method when the backing data for a given media item has * changed and the metadata should be updated immediately. */ public final void invalidateMediaSessionMetadata() { if (mediaMetadataProvider != null && player != null) { mediaSession.setMetadata(mediaMetadataProvider.getMetadata(player)); } }
private void releaseServiceUiAndStop() { if (isPlaying() || mPausedByTransientLossOfFocus || mPlayerHandler.hasMessages(TRACK_ENDED)) { return; } if (D) Log.d(TAG, "Nothing is playing anymore, releasing notification"); cancelNotification(); mAudioManager.abandonAudioFocus(mAudioFocusListener); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) mSession.setActive(false); if (!mServiceInUse) { saveQueue(true); stopSelf(mServiceStartId); } }
/** * Sets the {@link QueueEditor} to handle queue edits sent by the media controller. * * @param queueEditor The queue editor. */ public void setQueueEditor(QueueEditor queueEditor) { if (this.queueEditor != queueEditor) { unregisterCommandReceiver(this.queueEditor); this.queueEditor = queueEditor; registerCommandReceiver(queueEditor); mediaSession.setFlags( queueEditor == null ? BASE_MEDIA_SESSION_FLAGS : EDITOR_MEDIA_SESSION_FLAGS); } }
public MediaControllerCompatAssert isPlaying() { isNotNull(); assertThat(actual.getPlaybackState().getState()) // .overridingErrorMessage("Expected to be playing but was not.") // .isEqualTo(PlaybackStateCompat.STATE_PLAYING); return this; }
public MediaControllerCompatAssert hasActions(long flags) { isNotNull(); long actualFlags = actual.getPlaybackState().getActions(); assertThat(actualFlags) // .overridingErrorMessage("Expected control flags <%s> but was <%s>.", actionsToString(flags), actionsToString(actualFlags)) // .isEqualTo(flags); return this; }
/** * Creates an instance. * * <p>Equivalent to {@code MediaSessionConnector(mediaSession, playbackController, new * DefaultMediaMetadataProvider(mediaSession.getController(), null))}. * * @param mediaSession The {@link MediaSessionCompat} to connect to. * @param playbackController A {@link PlaybackController} for handling playback actions. */ public MediaSessionConnector( MediaSessionCompat mediaSession, PlaybackController playbackController) { this( mediaSession, playbackController, new DefaultMediaMetadataProvider(mediaSession.getController(), null)); }
private void publishFloatingQueueWindow() { if (callback.getQueueSize() == 0) { mediaSession.setQueue(Collections.emptyList()); activeQueueItemId = MediaSessionCompat.QueueItem.UNKNOWN_ID; return; } // Yes this is almost a copypasta, got a problem with that? =\ int windowCount = callback.getQueueSize(); int currentWindowIndex = callback.getCurrentPlayingIndex(); int queueSize = Math.min(maxQueueSize, windowCount); int startIndex = Util.constrainValue(currentWindowIndex - ((queueSize - 1) / 2), 0, windowCount - queueSize); List<MediaSessionCompat.QueueItem> queue = new ArrayList<>(); for (int i = startIndex; i < startIndex + queueSize; i++) { queue.add(new MediaSessionCompat.QueueItem(callback.getQueueMetadata(i), i)); } mediaSession.setQueue(queue); activeQueueItemId = currentWindowIndex; }
public MediaControllerCompatAssert hasPosition(long position) { isNotNull(); long actualPosition = actual.getPlaybackState().getPosition(); assertThat(actualPosition) // .overridingErrorMessage("Expected position <%s> but was <%s>.", position, actualPosition) // .isEqualTo(position); return this; }
public MediaControllerCompatAssert hasBufferPosition(long position) { isNotNull(); long actualPosition = actual.getPlaybackState().getBufferedPosition(); assertThat(actualPosition) // .overridingErrorMessage("Expected buffer position <%s> but was <%s>.", position, actualPosition) // .isEqualTo(position); return this; }
@Override public void onRemoveQueueItem(Player player, MediaDescriptionCompat description) { List<MediaSessionCompat.QueueItem> queue = mediaController.getQueue(); for (int i = 0; i < queue.size(); i++) { if (equalityChecker.equals(queue.get(i).getDescription(), description)) { queueDataAdapter.remove(i); queueMediaSource.removeMediaSource(i); return; } } }
@Override public PlaybackStateCompat.CustomAction getCustomAction() { CharSequence actionLabel; int iconResourceId; switch (player.getRepeatMode()) { case Player.REPEAT_MODE_ONE: actionLabel = repeatOneDescription; iconResourceId = R.drawable.exo_media_action_repeat_one; break; case Player.REPEAT_MODE_ALL: actionLabel = repeatAllDescription; iconResourceId = R.drawable.exo_media_action_repeat_all; break; case Player.REPEAT_MODE_OFF: default: actionLabel = repeatOffDescription; iconResourceId = R.drawable.exo_media_action_repeat_off; break; } PlaybackStateCompat.CustomAction.Builder repeatBuilder = new PlaybackStateCompat.CustomAction .Builder(ACTION_REPEAT_MODE, actionLabel, iconResourceId); return repeatBuilder.build(); }
@Override public void onShuffleModeEnabledChanged(boolean shuffleModeEnabled) { mediaSession.setShuffleMode( shuffleModeEnabled ? PlaybackStateCompat.SHUFFLE_MODE_ALL : PlaybackStateCompat.SHUFFLE_MODE_NONE); invalidateMediaSessionPlaybackState(); }
public MediaControllerCompatAssert isNotPlaying() { isNotNull(); assertThat(actual.getPlaybackState().getState()) // .overridingErrorMessage("Expected to not be playing but was.") // .isNotEqualTo(PlaybackStateCompat.STATE_PLAYING); return this; }
/** * Creates an instance. * * @param mediaSession The {@link MediaSessionCompat} to connect to. * @param playbackController A {@link PlaybackController} for handling playback actions, or {@code * null} if the connector should handle playback actions directly. * @param doMaintainMetadata Whether the connector should maintain the metadata of the session. * @param metadataExtrasPrefix A string to prefix extra keys which are propagated from the active * queue item to the session metadata. * @deprecated Use {@link MediaSessionConnector#MediaSessionConnector(MediaSessionCompat, * PlaybackController, MediaMetadataProvider)}. */ @Deprecated public MediaSessionConnector( MediaSessionCompat mediaSession, @Nullable PlaybackController playbackController, boolean doMaintainMetadata, @Nullable String metadataExtrasPrefix) { this( mediaSession, playbackController, doMaintainMetadata ? new DefaultMediaMetadataProvider(mediaSession.getController(), metadataExtrasPrefix) : null); }
private void publishFloatingQueueWindow(Player player) { if (player.getCurrentTimeline().isEmpty()) { mediaSession.setQueue(Collections.emptyList()); activeQueueItemId = MediaSessionCompat.QueueItem.UNKNOWN_ID; return; } int windowCount = player.getCurrentTimeline().getWindowCount(); int currentWindowIndex = player.getCurrentWindowIndex(); int queueSize = Math.min(maxQueueSize, windowCount); int startIndex = Util.constrainValue(currentWindowIndex - ((queueSize - 1) / 2), 0, windowCount - queueSize); List<MediaSessionCompat.QueueItem> queue = new ArrayList<>(); for (int i = startIndex; i < startIndex + queueSize; i++) { queue.add(new MediaSessionCompat.QueueItem(getMediaDescription(player, i), i)); } mediaSession.setQueue(queue); activeQueueItemId = currentWindowIndex; }