PlayerNotificationManager
@UnstableApi
public class PlayerNotificationManager
Starts, updates and cancels a media style notification reflecting the player state. The actions included in the notification can be customized along with their drawables, as described below.
The notification is cancelled when null is passed to setPlayer or when the notification is dismissed by the user.
If the player is released it must be removed from the manager by calling
setPlayer(null).
Action customization
Playback actions can be included or omitted as follows:usePlayPauseActions- Sets whether the play and pause actions are used.- Corresponding setter:
setUsePlayPauseActions - Default:
true
- Corresponding setter:
useRewindAction- Sets whether the rewind action is used.- Corresponding setter:
setUseRewindAction - Default:
true
- Corresponding setter:
useRewindActionInCompactView- IfuseRewindActionistrue, sets whether the rewind action is also used in compact view (including the lock screen notification). Else does nothing.- Corresponding setter:
setUseRewindActionInCompactView - Default:
false
- Corresponding setter:
useFastForwardAction- Sets whether the fast forward action is used.- Corresponding setter:
setUseFastForwardAction - Default:
true
- Corresponding setter:
useFastForwardActionInCompactView- IfuseFastForwardActionistrue, sets whether the fast forward action is also used in compact view (including the lock screen notification). Else does nothing.- Corresponding setter:
setUseFastForwardActionInCompactView - Default:
false
- Corresponding setter:
usePreviousAction- Whether the previous action is used.- Corresponding setter:
setUsePreviousAction - Default:
true
- Corresponding setter:
usePreviousActionInCompactView- IfusePreviousActionistrue, sets whether the previous action is also used in compact view (including the lock screen notification). Else does nothing.- Corresponding setter:
setUsePreviousActionInCompactView - Default:
false
- Corresponding setter:
useNextAction- Whether the next action is used.- Corresponding setter:
setUseNextAction - Default:
true
- Corresponding setter:
useNextActionInCompactView- IfuseNextActionistrue, sets whether the next action is also used in compact view (including the lock screen notification). Else does nothing.- Corresponding setter:
setUseNextActionInCompactView - Default:
false
- Corresponding setter:
useStopAction- Sets whether the stop action is used.- Corresponding setter:
setUseStopAction - Default:
false
- Corresponding setter:
Overriding drawables
The drawables used by PlayerNotificationManager can be overridden by drawables with the same names defined in your application. The drawables that can be overridden are:exo_notification_small_icon- The icon passed by default tosetSmallIcon. A different icon can also be specified programmatically by callingsetSmallIcon.exo_notification_play- The play icon.exo_notification_pause- The pause icon.exo_notification_rewind- The rewind icon.exo_notification_fastforward- The fast forward icon.exo_notification_previous- The previous icon.exo_notification_next- The next icon.exo_notification_stop- The stop icon.
Alternatively, the action icons can be set programmatically by using the Builder.
Unlike the drawables above, the large icon (i.e. the icon passed to setLargeIcon cannot be overridden in this way. Instead, the large icon is obtained from the MediaDescriptionAdapter passed to Builder.
Note: This class would require POST_NOTIFICATIONS permission if used without a media session.
Summary
Constants |
|
|---|---|
static final String |
ACTION_FAST_FORWARD = "androidx.media3.ui.notification.ffwd"The action which fast forwards. |
static final String |
ACTION_NEXT = "androidx.media3.ui.notification.next"The action which skips to the next media item. |
static final String |
ACTION_PAUSE = "androidx.media3.ui.notification.pause"The action which pauses playback. |
static final String |
ACTION_PLAY = "androidx.media3.ui.notification.play"The action which starts playback. |
static final String |
ACTION_PREVIOUS = "androidx.media3.ui.notification.prev"The action which skips to the previous media item. |
static final String |
ACTION_REWIND = "androidx.media3.ui.notification.rewind"The action which rewinds. |
static final String |
ACTION_STOP = "androidx.media3.ui.notification.stop"The action which stops playback. |
static final String |
EXTRA_INSTANCE_ID = "INSTANCE_ID"The extra key of the instance id of the player notification manager. |
Protected constructors |
|---|
PlayerNotificationManager( |
Public methods |
|
|---|---|
final void |
Forces an update of the notification if already started. |
final void |
setBadgeIconType(int badgeIconType)Sets the badge icon type of the notification. |
final void |
setColor(int color)Sets the accent color of the notification. |
final void |
setColorized(boolean colorized)Sets whether the notification should be colorized. |
final void |
setDefaults(int defaults)Sets the defaults. |
final void |
Sets the |
final void |
Sets the |
final void |
setPriority(@PlayerNotificationManager.Priority int priority)Sets the priority of the notification required for API 25 and lower. |
void |
setShowPlayButtonIfPlaybackIsSuppressed(Sets whether a play button is shown if playback is |
final void |
setSmallIcon(@DrawableRes int smallIconResourceId)Sets the small icon of the notification which is also shown in the system status bar. |
final void |
setUseChronometer(boolean useChronometer)Sets whether the elapsed time of the media playback should be displayed. |
final void |
setUseFastForwardAction(boolean useFastForwardAction)Sets whether the fast forward action should be used. |
final void |
setUseFastForwardActionInCompactView(Sets whether the fast forward action should also be used in compact view. |
final void |
setUseNextAction(boolean useNextAction)Sets whether the next action should be used. |
final void |
setUseNextActionInCompactView(boolean useNextActionInCompactView)If |
final void |
setUsePlayPauseActions(boolean usePlayPauseActions)Sets whether the play and pause actions should be used. |
final void |
setUsePreviousAction(boolean usePreviousAction)Sets whether the previous action should be used. |
final void |
setUsePreviousActionInCompactView(If |
final void |
setUseRewindAction(boolean useRewindAction)Sets whether the rewind action should be used. |
final void |
setUseRewindActionInCompactView(boolean useRewindActionInCompactView)Sets whether the rewind action should also be used in compact view. |
final void |
setUseStopAction(boolean useStopAction)Sets whether the stop action should be used. |
final void |
setVisibility(@PlayerNotificationManager.Visibility int visibility)Sets the visibility of the notification which determines whether and how the notification is shown when the device is in lock screen mode. |
Protected methods |
|
|---|---|
@Nullable NotificationCompat.Builder |
createNotification(Creates the notification given the current player state. |
int[] |
getActionIndicesForCompactView(List<String> actionNames, Player player)Gets an array with the indices of the buttons to be shown in compact mode. |
List<String> |
getActions(Player player)Gets the names and order of the actions to be included in the notification at the current player state. |
boolean |
getOngoing(Player player)Returns whether the generated notification should be ongoing. |
Constants
ACTION_FAST_FORWARD
public static final String ACTION_FAST_FORWARD = "androidx.media3.ui.notification.ffwd"
The action which fast forwards.
ACTION_NEXT
public static final String ACTION_NEXT = "androidx.media3.ui.notification.next"
The action which skips to the next media item.
ACTION_PAUSE
public static final String ACTION_PAUSE = "androidx.media3.ui.notification.pause"
The action which pauses playback.
ACTION_PLAY
public static final String ACTION_PLAY = "androidx.media3.ui.notification.play"
The action which starts playback.
ACTION_PREVIOUS
public static final String ACTION_PREVIOUS = "androidx.media3.ui.notification.prev"
The action which skips to the previous media item.
ACTION_REWIND
public static final String ACTION_REWIND = "androidx.media3.ui.notification.rewind"
The action which rewinds.
ACTION_STOP
public static final String ACTION_STOP = "androidx.media3.ui.notification.stop"
The action which stops playback.
EXTRA_INSTANCE_ID
public static final String EXTRA_INSTANCE_ID = "INSTANCE_ID"
The extra key of the instance id of the player notification manager.
Protected constructors
PlayerNotificationManager
protected PlayerNotificationManager(
Context context,
String channelId,
int notificationId,
PlayerNotificationManager.MediaDescriptionAdapter mediaDescriptionAdapter,
@Nullable PlayerNotificationManager.NotificationListener notificationListener,
@Nullable PlayerNotificationManager.CustomActionReceiver customActionReceiver,
int smallIconResourceId,
int playActionIconResourceId,
int pauseActionIconResourceId,
int stopActionIconResourceId,
int rewindActionIconResourceId,
int fastForwardActionIconResourceId,
int previousActionIconResourceId,
int nextActionIconResourceId,
@Nullable String groupKey
)
Public methods
setBadgeIconType
public final void setBadgeIconType(int badgeIconType)
Sets the badge icon type of the notification.
See setBadgeIconType.
| Parameters | |
|---|---|
int badgeIconType |
The badge icon type. |
setColor
public final void setColor(int color)
Sets the accent color of the notification.
See setColor.
| Parameters | |
|---|---|
int color |
The color, in ARGB integer form like the constants in |
setColorized
public final void setColorized(boolean colorized)
Sets whether the notification should be colorized. When set, the color set with setColor will be used as the background color for the notification.
See setColorized.
| Parameters | |
|---|---|
boolean colorized |
Whether to colorize the notification. |
setDefaults
public final void setDefaults(int defaults)
Sets the defaults.
See setDefaults.
| Parameters | |
|---|---|
int defaults |
The default notification options. |
setMediaSessionToken
public final void setMediaSessionToken(MediaSession.Token token)
Sets the MediaSession.Token.
When using MediaSessionCompat, this token can be obtained with
(MediaSession.Token) compatToken.getToken().
| Parameters | |
|---|---|
MediaSession.Token token |
The |
setPlayer
public final void setPlayer(@Nullable Player player)
Sets the Player.
Setting the player starts a notification immediately unless the player is in STATE_IDLE, in which case the notification is started as soon as the player transitions away from being idle.
If the player is released it must be removed from the manager by calling
setPlayer(null). This will cancel the notification.
setPriority
public final void setPriority(@PlayerNotificationManager.Priority int priority)
Sets the priority of the notification required for API 25 and lower.
See setPriority.
To set the priority for API levels above 25, you can create your own NotificationChannel with a given importance level and pass the id of the channel to Builder.
| Parameters | |
|---|---|
@PlayerNotificationManager.Priority int priority |
The priority which can be one of |
setShowPlayButtonIfPlaybackIsSuppressed
public void setShowPlayButtonIfPlaybackIsSuppressed(
boolean showPlayButtonIfSuppressed
)
Sets whether a play button is shown if playback is suppressed.
The default is true.
| Parameters | |
|---|---|
boolean showPlayButtonIfSuppressed |
Whether to show a play button if playback is |
setSmallIcon
public final void setSmallIcon(@DrawableRes int smallIconResourceId)
Sets the small icon of the notification which is also shown in the system status bar.
See setSmallIcon.
| Parameters | |
|---|---|
@DrawableRes int smallIconResourceId |
The resource id of the small icon. |
setUseChronometer
public final void setUseChronometer(boolean useChronometer)
Sets whether the elapsed time of the media playback should be displayed.
Note that this setting only works if all of the following are true:
- The media is
actively playing. - The media is not
dynamically changing its duration(like for example a live stream). - The media is not
interrupted by an ad. - The media is played at
regular speed.
See setUsesChronometer.
| Parameters | |
|---|---|
boolean useChronometer |
Whether to use chronometer. |
setUseFastForwardAction
public final void setUseFastForwardAction(boolean useFastForwardAction)
Sets whether the fast forward action should be used.
| Parameters | |
|---|---|
boolean useFastForwardAction |
Whether to use the fast forward action. |
setUseFastForwardActionInCompactView
public final void setUseFastForwardActionInCompactView(
boolean useFastForwardActionInCompactView
)
Sets whether the fast forward action should also be used in compact view. Has no effect if ACTION_FAST_FORWARD is not enabled, for instance if the media is not seekable.
If set to true, setUseNextActionInCompactView is set to false.
| Parameters | |
|---|---|
boolean useFastForwardActionInCompactView |
Whether to use the fast forward action in compact view. |
setUseNextAction
public final void setUseNextAction(boolean useNextAction)
Sets whether the next action should be used.
| Parameters | |
|---|---|
boolean useNextAction |
Whether to use the next action. |
setUseNextActionInCompactView
public final void setUseNextActionInCompactView(boolean useNextActionInCompactView)
If useNextAction is true, sets whether the next action should also be used in compact view. Has no effect if useNextAction is false.
If set to true, setUseFastForwardActionInCompactView is set to false.
| Parameters | |
|---|---|
boolean useNextActionInCompactView |
Whether to use the next action in compact view. |
setUsePlayPauseActions
public final void setUsePlayPauseActions(boolean usePlayPauseActions)
Sets whether the play and pause actions should be used.
| Parameters | |
|---|---|
boolean usePlayPauseActions |
Whether to use play and pause actions. |
setUsePreviousAction
public final void setUsePreviousAction(boolean usePreviousAction)
Sets whether the previous action should be used.
| Parameters | |
|---|---|
boolean usePreviousAction |
Whether to use the previous action. |
setUsePreviousActionInCompactView
public final void setUsePreviousActionInCompactView(
boolean usePreviousActionInCompactView
)
If usePreviousAction is true, sets whether the previous action should also be used in compact view. Has no effect if usePreviousAction is false.
If set to true, setUseRewindActionInCompactView is set to false.
| Parameters | |
|---|---|
boolean usePreviousActionInCompactView |
Whether to use the previous action in compact view. |
setUseRewindAction
public final void setUseRewindAction(boolean useRewindAction)
Sets whether the rewind action should be used.
| Parameters | |
|---|---|
boolean useRewindAction |
Whether to use the rewind action. |
setUseRewindActionInCompactView
public final void setUseRewindActionInCompactView(boolean useRewindActionInCompactView)
Sets whether the rewind action should also be used in compact view. Has no effect if ACTION_REWIND is not enabled, for instance if the media is not seekable.
If set to true, setUsePreviousActionInCompactView is set to false.
| Parameters | |
|---|---|
boolean useRewindActionInCompactView |
Whether to use the rewind action in compact view. |
setUseStopAction
public final void setUseStopAction(boolean useStopAction)
Sets whether the stop action should be used.
| Parameters | |
|---|---|
boolean useStopAction |
Whether to use the stop action. |
setVisibility
public final void setVisibility(@PlayerNotificationManager.Visibility int visibility)
Sets the visibility of the notification which determines whether and how the notification is shown when the device is in lock screen mode.
See setVisibility.
| Parameters | |
|---|---|
@PlayerNotificationManager.Visibility int visibility |
The visibility which must be one of |
Protected methods
createNotification
protected @Nullable NotificationCompat.Builder createNotification(
Player player,
@Nullable NotificationCompat.Builder builder,
boolean ongoing,
@Nullable Bitmap largeIcon
)
Creates the notification given the current player state.
| Parameters | |
|---|---|
Player player |
The player for which state to build a notification. |
@Nullable NotificationCompat.Builder builder |
The builder used to build the last notification, or |
boolean ongoing |
Whether the notification should be ongoing. |
@Nullable Bitmap largeIcon |
The large icon to be used. |
| Returns | |
|---|---|
@Nullable NotificationCompat.Builder |
The |
getActionIndicesForCompactView
protected int[] getActionIndicesForCompactView(List<String> actionNames, Player player)
Gets an array with the indices of the buttons to be shown in compact mode.
This method can be overridden. The indices must refer to the list of actions passed as the first parameter.
getActions
protected List<String> getActions(Player player)
Gets the names and order of the actions to be included in the notification at the current player state.
The playback and custom actions are combined and placed in the following order if not omitted:
+------------------------------------------------------------------------+ | prev | << | play/pause | >> | next | custom actions | stop | +------------------------------------------------------------------------+
This method can be safely overridden. However, the names must be of the playback actions ACTION_PAUSE, ACTION_PLAY, ACTION_FAST_FORWARD, ACTION_REWIND, ACTION_NEXT or ACTION_PREVIOUS, or a key contained in the map returned by createCustomActions. Otherwise the action name is ignored.
getOngoing
protected boolean getOngoing(Player player)
Returns whether the generated notification should be ongoing.