Class PlayerNotificationManager
- java.lang.Object
-
- com.google.android.exoplayer2.ui.PlayerNotificationManager
-
public class PlayerNotificationManager extends Object
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
nullis passed tosetPlayer(Player)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(boolean) - Default:
true
- Corresponding setter:
rewindIncrementMs- Sets the rewind increment. If set to zero the rewind action is not used.- Corresponding setter:
setControlDispatcher(ControlDispatcher) - Default:
DefaultControlDispatcher.DEFAULT_REWIND_MS(5000)
- Corresponding setter:
fastForwardIncrementMs- Sets the fast forward increment. If set to zero the fast forward action is not used.- Corresponding setter:
setControlDispatcher(ControlDispatcher) - Default:
DefaultControlDispatcher.DEFAULT_FAST_FORWARD_MS(15000)
- Corresponding setter:
usePreviousAction- Whether the previous action is used.- Corresponding setter:
setUsePreviousAction(boolean) - 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(boolean) - Default:
false
- Corresponding setter:
useNextAction- Whether the next action is used.- Corresponding setter:
setUseNextAction(boolean) - 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(boolean) - Default:
false
- Corresponding setter:
useStopAction- Sets whether the stop action is used.- Corresponding setter:
setUseStopAction(boolean) - 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 toNotificationCompat.Builder.setSmallIcon(int). A different icon can also be specified programmatically by callingsetSmallIcon(int).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 programatically by using the
PlayerNotificationManager.Builder.Unlike the drawables above, the large icon (i.e. the icon passed to
NotificationCompat.Builder.setLargeIcon(Bitmap)cannot be overridden in this way. Instead, the large icon is obtained from thePlayerNotificationManager.MediaDescriptionAdapterpassed toBuilder(Context, int, String, MediaDescriptionAdapter).
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description classPlayerNotificationManager.BitmapCallbackReceives aBitmap.static classPlayerNotificationManager.BuilderA builder forPlayerNotificationManagerinstances.static interfacePlayerNotificationManager.CustomActionReceiverDefines and handles custom actions.static interfacePlayerNotificationManager.MediaDescriptionAdapterAn adapter to provide content assets of the media currently playing.static interfacePlayerNotificationManager.NotificationListenerA listener for changes to the notification.static interfacePlayerNotificationManager.PriorityPriority of the notification (required for API 25 and lower).static interfacePlayerNotificationManager.VisibilityVisibility of notification on the lock screen.
-
Field Summary
Fields Modifier and Type Field Description static StringACTION_FAST_FORWARDThe action which fast forwards.static StringACTION_NEXTThe action which skips to the next window.static StringACTION_PAUSEThe action which pauses playback.static StringACTION_PLAYThe action which starts playback.static StringACTION_PREVIOUSThe action which skips to the previous window.static StringACTION_REWINDThe action which rewinds.static StringACTION_STOPThe action which stops playback.static StringEXTRA_INSTANCE_IDThe extra key of the instance id of the player notification manager.
-
Constructor Summary
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected androidx.core.app.NotificationCompat.BuildercreateNotification(Player player, androidx.core.app.NotificationCompat.Builder builder, boolean ongoing, Bitmap largeIcon)Creates the notification given the current player state.static PlayerNotificationManagercreateWithNotificationChannel(Context context, String channelId, int channelName, int channelDescription, int notificationId, PlayerNotificationManager.MediaDescriptionAdapter mediaDescriptionAdapter)Deprecated.Use thePlayerNotificationManager.Builderinstead.static PlayerNotificationManagercreateWithNotificationChannel(Context context, String channelId, int channelName, int channelDescription, int notificationId, PlayerNotificationManager.MediaDescriptionAdapter mediaDescriptionAdapter, PlayerNotificationManager.NotificationListener notificationListener)Deprecated.Use thePlayerNotificationManager.Builderinstead.static PlayerNotificationManagercreateWithNotificationChannel(Context context, String channelId, int channelName, int notificationId, PlayerNotificationManager.MediaDescriptionAdapter mediaDescriptionAdapter)Deprecated.Use thePlayerNotificationManager.Builderinstead.static PlayerNotificationManagercreateWithNotificationChannel(Context context, String channelId, int channelName, int notificationId, PlayerNotificationManager.MediaDescriptionAdapter mediaDescriptionAdapter, PlayerNotificationManager.NotificationListener notificationListener)Deprecated.Use thePlayerNotificationManager.Builderinstead.protected int[]getActionIndicesForCompactView(List<String> actionNames, Player player)Gets an array with the indices of the buttons to be shown in compact mode.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.protected booleangetOngoing(Player player)Returns whether the generated notification should be ongoing.voidinvalidate()Forces an update of the notification if already started.voidsetBadgeIconType(int badgeIconType)Sets the badge icon type of the notification.voidsetColor(int color)Sets the accent color of the notification.voidsetColorized(boolean colorized)Sets whether the notification should be colorized.voidsetControlDispatcher(ControlDispatcher controlDispatcher)Sets theControlDispatcher.voidsetDefaults(int defaults)Sets the defaults.voidsetFastForwardIncrementMs(long fastForwardMs)Deprecated.voidsetMediaSessionToken(android.support.v4.media.session.MediaSessionCompat.Token token)Sets theMediaSessionCompat.Token.voidsetPlaybackPreparer(PlaybackPreparer playbackPreparer)Deprecated.UsesetControlDispatcher(ControlDispatcher)instead.voidsetPlayer(Player player)Sets thePlayer.voidsetPriority(int priority)Sets the priority of the notification required for API 25 and lower.voidsetRewindIncrementMs(long rewindMs)Deprecated.voidsetSmallIcon(int smallIconResourceId)Sets the small icon of the notification which is also shown in the system status bar.voidsetUseChronometer(boolean useChronometer)Sets whether the elapsed time of the media playback should be displayed.voidsetUseNavigationActions(boolean useNavigationActions)Deprecated.voidsetUseNavigationActionsInCompactView(boolean useNavigationActionsInCompactView)Deprecated.voidsetUseNextAction(boolean useNextAction)Sets whether the next action should be used.voidsetUseNextActionInCompactView(boolean useNextActionInCompactView)IfuseNextActionistrue, sets whether the next action should also be used in compact view.voidsetUsePlayPauseActions(boolean usePlayPauseActions)Sets whether the play and pause actions should be used.voidsetUsePreviousAction(boolean usePreviousAction)Sets whether the previous action should be used.voidsetUsePreviousActionInCompactView(boolean usePreviousActionInCompactView)IfusePreviousActionistrue, sets whether the previous action should also be used in compact view.voidsetUseStopAction(boolean useStopAction)Sets whether the stop action should be used.voidsetVisibility(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.
-
-
-
Field Detail
-
ACTION_PLAY
public static final String ACTION_PLAY
The action which starts playback.- See Also:
- Constant Field Values
-
ACTION_PAUSE
public static final String ACTION_PAUSE
The action which pauses playback.- See Also:
- Constant Field Values
-
ACTION_PREVIOUS
public static final String ACTION_PREVIOUS
The action which skips to the previous window.- See Also:
- Constant Field Values
-
ACTION_NEXT
public static final String ACTION_NEXT
The action which skips to the next window.- See Also:
- Constant Field Values
-
ACTION_FAST_FORWARD
public static final String ACTION_FAST_FORWARD
The action which fast forwards.- See Also:
- Constant Field Values
-
ACTION_REWIND
public static final String ACTION_REWIND
The action which rewinds.- See Also:
- Constant Field Values
-
ACTION_STOP
public static final String ACTION_STOP
The action which stops playback.- See Also:
- Constant Field Values
-
EXTRA_INSTANCE_ID
public static final String EXTRA_INSTANCE_ID
The extra key of the instance id of the player notification manager.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
PlayerNotificationManager
@Deprecated public PlayerNotificationManager(Context context, String channelId, int notificationId, PlayerNotificationManager.MediaDescriptionAdapter mediaDescriptionAdapter)
Deprecated.Use thePlayerNotificationManager.Builderinstead.
-
PlayerNotificationManager
@Deprecated public PlayerNotificationManager(Context context, String channelId, int notificationId, PlayerNotificationManager.MediaDescriptionAdapter mediaDescriptionAdapter, @Nullable PlayerNotificationManager.NotificationListener notificationListener)
Deprecated.Use thePlayerNotificationManager.Builderinstead.
-
PlayerNotificationManager
@Deprecated public PlayerNotificationManager(Context context, String channelId, int notificationId, PlayerNotificationManager.MediaDescriptionAdapter mediaDescriptionAdapter, @Nullable PlayerNotificationManager.CustomActionReceiver customActionReceiver)
Deprecated.Use thePlayerNotificationManager.Builderinstead.
-
PlayerNotificationManager
@Deprecated public PlayerNotificationManager(Context context, String channelId, int notificationId, PlayerNotificationManager.MediaDescriptionAdapter mediaDescriptionAdapter, @Nullable PlayerNotificationManager.NotificationListener notificationListener, @Nullable PlayerNotificationManager.CustomActionReceiver customActionReceiver)
Deprecated.Use thePlayerNotificationManager.Builderinstead.
-
-
Method Detail
-
createWithNotificationChannel
@Deprecated public static PlayerNotificationManager createWithNotificationChannel(Context context, String channelId, @StringRes int channelName, int notificationId, PlayerNotificationManager.MediaDescriptionAdapter mediaDescriptionAdapter)
Deprecated.Use thePlayerNotificationManager.Builderinstead.
-
createWithNotificationChannel
@Deprecated public static PlayerNotificationManager createWithNotificationChannel(Context context, String channelId, @StringRes int channelName, @StringRes int channelDescription, int notificationId, PlayerNotificationManager.MediaDescriptionAdapter mediaDescriptionAdapter)
Deprecated.Use thePlayerNotificationManager.Builderinstead.
-
createWithNotificationChannel
@Deprecated public static PlayerNotificationManager createWithNotificationChannel(Context context, String channelId, @StringRes int channelName, int notificationId, PlayerNotificationManager.MediaDescriptionAdapter mediaDescriptionAdapter, @Nullable PlayerNotificationManager.NotificationListener notificationListener)
Deprecated.Use thePlayerNotificationManager.Builderinstead.
-
createWithNotificationChannel
@Deprecated public static PlayerNotificationManager createWithNotificationChannel(Context context, String channelId, @StringRes int channelName, @StringRes int channelDescription, int notificationId, PlayerNotificationManager.MediaDescriptionAdapter mediaDescriptionAdapter, @Nullable PlayerNotificationManager.NotificationListener notificationListener)
Deprecated.Use thePlayerNotificationManager.Builderinstead.
-
setPlayer
public final void setPlayer(@Nullable Player player)Sets thePlayer.Setting the player starts a notification immediately unless the player is in
Player.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.- Parameters:
player- ThePlayerto use, ornullto remove the current player. Only players which are accessed on the main thread are supported (player.getApplicationLooper() == Looper.getMainLooper()).
-
setPlaybackPreparer
@Deprecated public void setPlaybackPreparer(@Nullable PlaybackPreparer playbackPreparer)
Deprecated.UsesetControlDispatcher(ControlDispatcher)instead. The manager callsControlDispatcher.dispatchPrepare(Player)instead ofPlaybackPreparer.preparePlayback(). TheDefaultControlDispatcherthat this manager uses by default, callsPlayer.prepare(). If you wish to intercept or customize this behaviour, you can provide a custom implementation ofControlDispatcher.dispatchPrepare(Player)and pass it tosetControlDispatcher(ControlDispatcher).
-
setControlDispatcher
public final void setControlDispatcher(ControlDispatcher controlDispatcher)
Sets theControlDispatcher.- Parameters:
controlDispatcher- TheControlDispatcher.
-
setFastForwardIncrementMs
@Deprecated public final void setFastForwardIncrementMs(long fastForwardMs)
Deprecated.
-
setRewindIncrementMs
@Deprecated public final void setRewindIncrementMs(long rewindMs)
Deprecated.
-
setUseNextAction
public void setUseNextAction(boolean useNextAction)
Sets whether the next action should be used.- Parameters:
useNextAction- Whether to use the next action.
-
setUsePreviousAction
public void setUsePreviousAction(boolean usePreviousAction)
Sets whether the previous action should be used.- Parameters:
usePreviousAction- Whether to use the previous action.
-
setUseNavigationActions
@Deprecated public final void setUseNavigationActions(boolean useNavigationActions)
Deprecated.Sets whether the navigation actions should be used.- Parameters:
useNavigationActions- Whether to use navigation actions.
-
setUseNextActionInCompactView
public void setUseNextActionInCompactView(boolean useNextActionInCompactView)
IfuseNextActionistrue, sets whether the next action should also be used in compact view. Has no effect ifuseNextActionisfalse.- Parameters:
useNextActionInCompactView- Whether to use the next action in compact view.
-
setUsePreviousActionInCompactView
public void setUsePreviousActionInCompactView(boolean usePreviousActionInCompactView)
IfusePreviousActionistrue, sets whether the previous action should also be used in compact view. Has no effect ifusePreviousActionisfalse.- Parameters:
usePreviousActionInCompactView- Whether to use the previous action in compact view.
-
setUseNavigationActionsInCompactView
@Deprecated public final void setUseNavigationActionsInCompactView(boolean useNavigationActionsInCompactView)
Deprecated.IfuseNavigationActionsistrue, sets whether navigation actions should also be used in compact view. Has no effect ifuseNavigationActionsisfalse.- Parameters:
useNavigationActionsInCompactView- Whether to use navigation actions in compact view.
-
setUsePlayPauseActions
public final void setUsePlayPauseActions(boolean usePlayPauseActions)
Sets whether the play and pause actions should be used.- Parameters:
usePlayPauseActions- Whether to use play and pause actions.
-
setUseStopAction
public final void setUseStopAction(boolean useStopAction)
Sets whether the stop action should be used.- Parameters:
useStopAction- Whether to use the stop action.
-
setMediaSessionToken
public final void setMediaSessionToken(android.support.v4.media.session.MediaSessionCompat.Token token)
Sets theMediaSessionCompat.Token.- Parameters:
token- TheMediaSessionCompat.Token.
-
setBadgeIconType
public final void setBadgeIconType(int badgeIconType)
Sets the badge icon type of the notification.See
NotificationCompat.Builder.setBadgeIconType(int).- Parameters:
badgeIconType- The badge icon type.
-
setColorized
public final void setColorized(boolean colorized)
Sets whether the notification should be colorized. When set, the color set withsetColor(int)will be used as the background color for the notification.See
NotificationCompat.Builder.setColorized(boolean).- Parameters:
colorized- Whether to colorize the notification.
-
setDefaults
public final void setDefaults(int defaults)
Sets the defaults.See
NotificationCompat.Builder.setDefaults(int).- Parameters:
defaults- The default notification options.
-
setColor
public final void setColor(int color)
Sets the accent color of the notification.See
NotificationCompat.Builder.setColor(int).- Parameters:
color- The color, in ARGB integer form like the constants inColor.
-
setPriority
public final void setPriority(@Priority int priority)
Sets the priority of the notification required for API 25 and lower.See
NotificationCompat.Builder.setPriority(int).To set the priority for API levels above 25, you can create your own
NotificationChannelwith a given importance level and pass the id of the channel to theconstructor.- Parameters:
priority- The priority which can be one ofNotificationCompat.PRIORITY_DEFAULT,NotificationCompat.PRIORITY_MAX,NotificationCompat.PRIORITY_HIGH,NotificationCompat.PRIORITY_LOWorNotificationCompat.PRIORITY_MIN. If not setNotificationCompat.PRIORITY_LOWis used by default.
-
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
NotificationCompat.Builder.setSmallIcon(int).- Parameters:
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. - The device is running at least API 21 (Lollipop).
See
NotificationCompat.Builder.setUsesChronometer(boolean).- Parameters:
useChronometer- Whether to use chronometer.
- The media is
-
setVisibility
public final void setVisibility(@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
NotificationCompat.Builder.setVisibility(int).- Parameters:
visibility- The visibility which must be one ofNotificationCompat.VISIBILITY_PUBLIC,NotificationCompat.VISIBILITY_PRIVATEorNotificationCompat.VISIBILITY_SECRET.
-
invalidate
public void invalidate()
Forces an update of the notification if already started.
-
createNotification
@Nullable protected androidx.core.app.NotificationCompat.Builder createNotification(Player player, @Nullable androidx.core.app.NotificationCompat.Builder builder, boolean ongoing, @Nullable Bitmap largeIcon)
Creates the notification given the current player state.- Parameters:
player- The player for which state to build a notification.builder- The builder used to build the last notification, ornull. Re-using the builder when possible can prevent notification flicker whenUtil#SDK_INT< 21.ongoing- Whether the notification should be ongoing.largeIcon- The large icon to be used.- Returns:
- The
NotificationCompat.Builderon which to callNotificationCompat.Builder.build()to obtain the notification, ornullif no notification should be displayed.
-
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_NEXTorACTION_PREVIOUS, or a key contained in the map returned byPlayerNotificationManager.CustomActionReceiver.createCustomActions(Context, int). Otherwise the action name is ignored.
-
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.
- Parameters:
actionNames- The names of the actions included in the notification.player- The player for which a notification is being built.
-
getOngoing
protected boolean getOngoing(Player player)
Returns whether the generated notification should be ongoing.
-
-