Class MediaSessionConnector
- java.lang.Object
-
- com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector
-
@Deprecated public final class MediaSessionConnector extends Object
Deprecated.com.google.android.exoplayer2 is deprecated. Please migrate to androidx.media3 (which contains the same ExoPlayer code). See the migration guide for more details, including a script to help with the migration.Connects aMediaSessionCompat
to aPlayer
.This connector does not call
MediaSessionCompat.setActive(boolean)
, and so application code is responsible for making the session active when desired. A session must be active for transport controls to be displayed (e.g. on the lock screen) and for it to receive media button events.The connector listens for actions sent by the media session's controller and implements these actions by calling appropriate player methods. The playback state of the media session is automatically synced with the player. The connector can also be optionally extended by providing various collaborators:
- Actions to initiate media playback (
PlaybackStateCompat#ACTION_PREPARE_*
andPlaybackStateCompat#ACTION_PLAY_*
) can be handled by aMediaSessionConnector.PlaybackPreparer
passed tosetPlaybackPreparer(PlaybackPreparer)
. - Custom actions can be handled by passing one or more
MediaSessionConnector.CustomActionProvider
s tosetCustomActionProviders(CustomActionProvider...)
. - To enable a media queue and navigation within it, you can set a
MediaSessionConnector.QueueNavigator
by callingsetQueueNavigator(QueueNavigator)
. Use ofTimelineQueueNavigator
is recommended for most use cases. - To enable editing of the media queue, you can set a
MediaSessionConnector.QueueEditor
by callingsetQueueEditor(QueueEditor)
. - A
MediaSessionConnector.MediaButtonEventHandler
can be set by callingsetMediaButtonEventHandler(MediaButtonEventHandler)
. By default media button events are handled byMediaSessionCompat.Callback.onMediaButtonEvent(Intent)
. - An
ErrorMessageProvider
for providing human readable error messages and corresponding error codes can be set by callingsetErrorMessageProvider(ErrorMessageProvider)
. - A
MediaSessionConnector.MediaMetadataProvider
can be set by callingsetMediaMetadataProvider(MediaMetadataProvider)
. By default theMediaSessionConnector.DefaultMediaMetadataProvider
is used.
- Actions to initiate media playback (
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
MediaSessionConnector.CaptionCallback
Deprecated.Handles requests for enabling or disabling captions.static interface
MediaSessionConnector.CommandReceiver
Deprecated.Receiver of media commands sent by a media controller.static interface
MediaSessionConnector.CustomActionProvider
Deprecated.Provides aPlaybackStateCompat.CustomAction
to be published and handles the action when sent by a media controller.static class
MediaSessionConnector.DefaultMediaMetadataProvider
Deprecated.Provides a defaultMediaMetadataCompat
with properties and extras taken from theMediaDescriptionCompat
of theMediaSessionCompat.QueueItem
of the active queue item.static interface
MediaSessionConnector.MediaButtonEventHandler
Deprecated.Handles a media button event.static interface
MediaSessionConnector.MediaMetadataProvider
Deprecated.Provides aMediaMetadataCompat
for a given player state.static interface
MediaSessionConnector.PlaybackActions
Deprecated.Playback actions supported by the connector.static interface
MediaSessionConnector.PlaybackPreparer
Deprecated.Interface to which playback preparation and play actions are delegated.static interface
MediaSessionConnector.QueueEditor
Deprecated.Handles media session queue edits.static interface
MediaSessionConnector.QueueNavigator
Deprecated.Handles queue navigation actions, and updates the media session queue by callingMediaSessionCompat.setQueue()
.static interface
MediaSessionConnector.RatingCallback
Deprecated.Callback receiving a user rating for the active media item.
-
Field Summary
Fields Modifier and Type Field Description static long
ALL_PLAYBACK_ACTIONS
Deprecated.static long
DEFAULT_PLAYBACK_ACTIONS
Deprecated.The default playback actions.static String
EXTRAS_SPEED
Deprecated.The name of thePlaybackStateCompat
float extra with the value ofPlayer.getPlaybackParameters().speed
.android.support.v4.media.session.MediaSessionCompat
mediaSession
Deprecated.The wrappedMediaSessionCompat
.
-
Constructor Summary
Constructors Constructor Description MediaSessionConnector(android.support.v4.media.session.MediaSessionCompat mediaSession)
Deprecated.Creates an instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
invalidateMediaSessionMetadata()
Deprecated.Updates the metadata of the media session.void
invalidateMediaSessionPlaybackState()
Deprecated.Updates the playback state of the media session.void
invalidateMediaSessionQueue()
Deprecated.Updates the queue of the media session by callingMediaSessionConnector.QueueNavigator.onTimelineChanged(Player)
.void
registerCustomCommandReceiver(MediaSessionConnector.CommandReceiver commandReceiver)
Deprecated.Registers a custom command receiver for responding to commands delivered viaMediaSessionCompat.Callback.onCommand(String, Bundle, ResultReceiver)
.void
setCaptionCallback(MediaSessionConnector.CaptionCallback captionCallback)
Deprecated.Sets theMediaSessionConnector.CaptionCallback
to handle requests to enable or disable captions.void
setClearMediaItemsOnStop(boolean clearMediaItemsOnStop)
Deprecated.Sets whether media items are cleared from the playlist when a client sends aMediaControllerCompat.TransportControls.stop()
command.void
setCustomActionProviders(MediaSessionConnector.CustomActionProvider... customActionProviders)
Deprecated.Sets custom action providers.void
setCustomErrorMessage(CharSequence message)
Deprecated.Sets a custom error on the session.void
setCustomErrorMessage(CharSequence message, int code)
Deprecated.Sets a custom error on the session.void
setCustomErrorMessage(CharSequence message, int code, Bundle extras)
Deprecated.Sets a custom error on the session.void
setDispatchUnsupportedActionsEnabled(boolean dispatchUnsupportedActionsEnabled)
Deprecated.Sets whether actions that are not advertised to theMediaSessionCompat
will be dispatched either way.void
setEnabledPlaybackActions(long enabledPlaybackActions)
Deprecated.Sets the enabled playback actions.void
setErrorMessageProvider(ErrorMessageProvider<? super PlaybackException> errorMessageProvider)
Deprecated.Sets the optionalErrorMessageProvider
.void
setMapStateIdleToSessionStateStopped(boolean mapIdleToStopped)
Deprecated.Sets whetherPlayer.STATE_IDLE
should be mapped toPlaybackStateCompat.STATE_STOPPED
.void
setMediaButtonEventHandler(MediaSessionConnector.MediaButtonEventHandler mediaButtonEventHandler)
Deprecated.void
setMediaMetadataProvider(MediaSessionConnector.MediaMetadataProvider mediaMetadataProvider)
Deprecated.Sets a provider of metadata to be published to the media session.void
setMetadataDeduplicationEnabled(boolean metadataDeduplicationEnabled)
Deprecated.Sets whetherMediaSessionConnector.MediaMetadataProvider.sameAs(MediaMetadataCompat, MediaMetadataCompat)
should be consulted before callingMediaSessionCompat.setMetadata(MediaMetadataCompat)
.void
setPlaybackPreparer(MediaSessionConnector.PlaybackPreparer playbackPreparer)
Deprecated.Sets theMediaSessionConnector.PlaybackPreparer
.void
setPlayer(Player player)
Deprecated.Sets the player to be connected to the media session.void
setQueueEditor(MediaSessionConnector.QueueEditor queueEditor)
Deprecated.Sets theMediaSessionConnector.QueueEditor
to handle queue edits sent by the media controller.void
setQueueNavigator(MediaSessionConnector.QueueNavigator queueNavigator)
Deprecated.Sets theMediaSessionConnector.QueueNavigator
to handle queue navigation actionsACTION_SKIP_TO_NEXT
,ACTION_SKIP_TO_PREVIOUS
andACTION_SKIP_TO_QUEUE_ITEM
.void
setRatingCallback(MediaSessionConnector.RatingCallback ratingCallback)
Deprecated.Sets theMediaSessionConnector.RatingCallback
to handle user ratings.void
unregisterCustomCommandReceiver(MediaSessionConnector.CommandReceiver commandReceiver)
Deprecated.Unregisters a previously registered custom command receiver.
-
-
-
Field Detail
-
ALL_PLAYBACK_ACTIONS
public static final long ALL_PLAYBACK_ACTIONS
Deprecated.- See Also:
- Constant Field Values
-
DEFAULT_PLAYBACK_ACTIONS
public static final long DEFAULT_PLAYBACK_ACTIONS
Deprecated.The default playback actions.- See Also:
- Constant Field Values
-
EXTRAS_SPEED
public static final String EXTRAS_SPEED
Deprecated.The name of thePlaybackStateCompat
float extra with the value ofPlayer.getPlaybackParameters().speed
.- See Also:
- Constant Field Values
-
mediaSession
public final android.support.v4.media.session.MediaSessionCompat mediaSession
Deprecated.The wrappedMediaSessionCompat
.
-
-
Method Detail
-
setPlayer
public void setPlayer(@Nullable Player player)
Deprecated.Sets the player to be connected to the media session. Must be called on the same thread that is used to access the player.- Parameters:
player
- The player to be connected to theMediaSession
, ornull
to disconnect the current player.
-
setPlaybackPreparer
public void setPlaybackPreparer(@Nullable MediaSessionConnector.PlaybackPreparer playbackPreparer)
Deprecated.Sets theMediaSessionConnector.PlaybackPreparer
.- Parameters:
playbackPreparer
- TheMediaSessionConnector.PlaybackPreparer
.
-
setMediaButtonEventHandler
public void setMediaButtonEventHandler(@Nullable MediaSessionConnector.MediaButtonEventHandler mediaButtonEventHandler)
Deprecated.Sets theMediaSessionConnector.MediaButtonEventHandler
. Passnull
if the media button event should be handled byMediaSessionCompat.Callback.onMediaButtonEvent(Intent)
.Please note that prior to API 21 MediaButton events are not delivered to the
MediaSessionCompat
. Instead they are delivered as key events (see 'Responding to media buttons'). In anActivity
, media button events arrive at theActivity.dispatchKeyEvent(KeyEvent)
method.If you are running the player in a foreground service (prior to API 21), you can create an intent filter and handle the
android.intent.action.MEDIA_BUTTON
action yourself. See Service handling ACTION_MEDIA_BUTTON for more information.- Parameters:
mediaButtonEventHandler
- TheMediaSessionConnector.MediaButtonEventHandler
, or null to let the event be handled byMediaSessionCompat.Callback.onMediaButtonEvent(Intent)
.
-
setEnabledPlaybackActions
public void setEnabledPlaybackActions(long enabledPlaybackActions)
Deprecated.Sets the enabled playback actions.- Parameters:
enabledPlaybackActions
- The enabled playback actions.
-
setErrorMessageProvider
public void setErrorMessageProvider(@Nullable ErrorMessageProvider<? super PlaybackException> errorMessageProvider)
Deprecated.Sets the optionalErrorMessageProvider
.- Parameters:
errorMessageProvider
- The error message provider.
-
setQueueNavigator
public void setQueueNavigator(@Nullable MediaSessionConnector.QueueNavigator queueNavigator)
Deprecated.Sets theMediaSessionConnector.QueueNavigator
to handle queue navigation actionsACTION_SKIP_TO_NEXT
,ACTION_SKIP_TO_PREVIOUS
andACTION_SKIP_TO_QUEUE_ITEM
.- Parameters:
queueNavigator
- The queue navigator.
-
setQueueEditor
public void setQueueEditor(@Nullable MediaSessionConnector.QueueEditor queueEditor)
Deprecated.Sets theMediaSessionConnector.QueueEditor
to handle queue edits sent by the media controller.- Parameters:
queueEditor
- The queue editor.
-
setRatingCallback
public void setRatingCallback(@Nullable MediaSessionConnector.RatingCallback ratingCallback)
Deprecated.Sets theMediaSessionConnector.RatingCallback
to handle user ratings.- Parameters:
ratingCallback
- The rating callback.
-
setCaptionCallback
public void setCaptionCallback(@Nullable MediaSessionConnector.CaptionCallback captionCallback)
Deprecated.Sets theMediaSessionConnector.CaptionCallback
to handle requests to enable or disable captions.- Parameters:
captionCallback
- The caption callback.
-
setCustomErrorMessage
public void setCustomErrorMessage(@Nullable CharSequence message)
Deprecated.Sets a custom error on the session.This sets the error code via
PlaybackStateCompat.Builder.setErrorMessage(int, CharSequence)
. By default, the error code will be set toPlaybackStateCompat.ERROR_CODE_APP_ERROR
.- Parameters:
message
- The error string to report ornull
to clear the error.
-
setCustomErrorMessage
public void setCustomErrorMessage(@Nullable CharSequence message, int code)
Deprecated.Sets a custom error on the session.- Parameters:
message
- The error string to report ornull
to clear the error.code
- The error code to report. Ignored whenmessage
isnull
.
-
setCustomErrorMessage
public void setCustomErrorMessage(@Nullable CharSequence message, int code, @Nullable Bundle extras)
Deprecated.Sets a custom error on the session.- Parameters:
message
- The error string to report ornull
to clear the error.code
- The error code to report. Ignored whenmessage
isnull
.extras
- Extras to include in reportedPlaybackStateCompat
.
-
setCustomActionProviders
public void setCustomActionProviders(@Nullable MediaSessionConnector.CustomActionProvider... customActionProviders)
Deprecated.Sets custom action providers. The order of theMediaSessionConnector.CustomActionProvider
s determines the order in which the actions are published.- Parameters:
customActionProviders
- The custom action providers, or null to remove all existing custom action providers.
-
setMediaMetadataProvider
public void setMediaMetadataProvider(@Nullable MediaSessionConnector.MediaMetadataProvider mediaMetadataProvider)
Deprecated.Sets a provider of metadata to be published to the media session. Passnull
if no metadata should be published.- Parameters:
mediaMetadataProvider
- The provider of metadata to publish, ornull
if no metadata should be published.
-
setDispatchUnsupportedActionsEnabled
public void setDispatchUnsupportedActionsEnabled(boolean dispatchUnsupportedActionsEnabled)
Deprecated.Sets whether actions that are not advertised to theMediaSessionCompat
will be dispatched either way. Default value is false.
-
setClearMediaItemsOnStop
public void setClearMediaItemsOnStop(boolean clearMediaItemsOnStop)
Deprecated.Sets whether media items are cleared from the playlist when a client sends aMediaControllerCompat.TransportControls.stop()
command.
-
setMapStateIdleToSessionStateStopped
public void setMapStateIdleToSessionStateStopped(boolean mapIdleToStopped)
Deprecated.Sets whetherPlayer.STATE_IDLE
should be mapped toPlaybackStateCompat.STATE_STOPPED
. The default is falsePlayer.STATE_IDLE
which maps toPlaybackStateCompat.STATE_NONE
.
-
setMetadataDeduplicationEnabled
public void setMetadataDeduplicationEnabled(boolean metadataDeduplicationEnabled)
Deprecated.Sets whetherMediaSessionConnector.MediaMetadataProvider.sameAs(MediaMetadataCompat, MediaMetadataCompat)
should be consulted before callingMediaSessionCompat.setMetadata(MediaMetadataCompat)
.Note that this comparison is normally only required when you are using media sources that may introduce duplicate updates of the metadata for the same media item (e.g. live streams).
- Parameters:
metadataDeduplicationEnabled
- Whether to deduplicate metadata objects on invalidation.
-
invalidateMediaSessionMetadata
public final void invalidateMediaSessionMetadata()
Deprecated.Updates the metadata of the media session.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.
The
MediaMetadataCompat
which is published to the session is obtained by callingMediaSessionConnector.MediaMetadataProvider.getMetadata(Player)
.
-
invalidateMediaSessionPlaybackState
public final void invalidateMediaSessionPlaybackState()
Deprecated.Updates the playback state of the media session.Apps normally only need to call this method when the custom actions provided by a
MediaSessionConnector.CustomActionProvider
changed and the playback state needs to be updated immediately.
-
invalidateMediaSessionQueue
public final void invalidateMediaSessionQueue()
Deprecated.Updates the queue of the media session by callingMediaSessionConnector.QueueNavigator.onTimelineChanged(Player)
.Apps normally only need to call this method when the backing data for a given queue item has changed and the queue should be updated immediately.
-
registerCustomCommandReceiver
public void registerCustomCommandReceiver(@Nullable MediaSessionConnector.CommandReceiver commandReceiver)
Deprecated.Registers a custom command receiver for responding to commands delivered viaMediaSessionCompat.Callback.onCommand(String, Bundle, ResultReceiver)
.Commands are only dispatched to this receiver when a player is connected.
- Parameters:
commandReceiver
- The command receiver to register.
-
unregisterCustomCommandReceiver
public void unregisterCustomCommandReceiver(@Nullable MediaSessionConnector.CommandReceiver commandReceiver)
Deprecated.Unregisters a previously registered custom command receiver.- Parameters:
commandReceiver
- The command receiver to unregister.
-
-