public class PlayerNotificationManager extends Object
The notification is cancelled when null is passed to setPlayer(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).
usePlayPauseActions - Sets whether the play and pause actions are used.
setUsePlayPauseActions(boolean)
true
rewindIncrementMs - Sets the rewind increment. If set to zero the rewind
action is not used.
setControlDispatcher(ControlDispatcher)
DefaultControlDispatcher.DEFAULT_REWIND_MS (5000)
fastForwardIncrementMs - Sets the fast forward increment. If set to zero the
fast forward action is not used.
setControlDispatcher(ControlDispatcher)
DefaultControlDispatcher.DEFAULT_FAST_FORWARD_MS (15000)
usePreviousAction - Whether the previous action is used.
setUsePreviousAction(boolean)
true
usePreviousActionInCompactView - If usePreviousAction is
true, sets whether the previous action is also used in compact view (including the lock
screen notification). Else does nothing.
setUsePreviousActionInCompactView(boolean)
false
useNextAction - Whether the next action is used.
setUseNextAction(boolean)
true
useNextActionInCompactView - If useNextAction is true, sets
whether the next action is also used in compact view (including the lock screen
notification). Else does nothing.
setUseNextActionInCompactView(boolean)
false
useStopAction - Sets whether the stop action is used.
setUseStopAction(boolean)
false
exo_notification_small_icon - The icon passed by default to NotificationCompat.Builder.setSmallIcon(int). A different icon can also be specified
programmatically by calling setSmallIcon(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.
NotificationCompat.Builder.setLargeIcon(Bitmap) cannot be overridden in this way. Instead, the
large icon is obtained from the PlayerNotificationManager.MediaDescriptionAdapter injected when creating the
PlayerNotificationManager.| Modifier and Type | Class | Description |
|---|---|---|
class |
PlayerNotificationManager.BitmapCallback |
Receives a
Bitmap. |
static interface |
PlayerNotificationManager.CustomActionReceiver |
Defines and handles custom actions.
|
static interface |
PlayerNotificationManager.MediaDescriptionAdapter |
An adapter to provide content assets of the media currently playing.
|
static interface |
PlayerNotificationManager.NotificationListener |
A listener for changes to the notification.
|
static interface |
PlayerNotificationManager.Priority |
Priority of the notification (required for API 25 and lower).
|
static interface |
PlayerNotificationManager.Visibility |
Visibility of notification on the lock screen.
|
| Modifier and Type | Field | Description |
|---|---|---|
static String |
ACTION_FAST_FORWARD |
The action which fast forwards.
|
static String |
ACTION_NEXT |
The action which skips to the next window.
|
static String |
ACTION_PAUSE |
The action which pauses playback.
|
static String |
ACTION_PLAY |
The action which starts playback.
|
static String |
ACTION_PREVIOUS |
The action which skips to the previous window.
|
static String |
ACTION_REWIND |
The action which rewinds.
|
static String |
ACTION_STOP |
The action which stops playback.
|
static String |
EXTRA_INSTANCE_ID |
The extra key of the instance id of the player notification manager.
|
| Constructor | Description |
|---|---|
PlayerNotificationManager(Context context,
String channelId,
int notificationId,
PlayerNotificationManager.MediaDescriptionAdapter mediaDescriptionAdapter) |
Creates a notification manager using the specified notification
channelId. |
PlayerNotificationManager(Context context,
String channelId,
int notificationId,
PlayerNotificationManager.MediaDescriptionAdapter mediaDescriptionAdapter,
PlayerNotificationManager.CustomActionReceiver customActionReceiver) |
Creates a notification manager using the specified notification
channelId and PlayerNotificationManager.CustomActionReceiver. |
PlayerNotificationManager(Context context,
String channelId,
int notificationId,
PlayerNotificationManager.MediaDescriptionAdapter mediaDescriptionAdapter,
PlayerNotificationManager.NotificationListener notificationListener) |
Creates a notification manager using the specified notification
channelId and PlayerNotificationManager.NotificationListener. |
PlayerNotificationManager(Context context,
String channelId,
int notificationId,
PlayerNotificationManager.MediaDescriptionAdapter mediaDescriptionAdapter,
PlayerNotificationManager.NotificationListener notificationListener,
PlayerNotificationManager.CustomActionReceiver customActionReceiver) |
Creates a notification manager using the specified notification
channelId, PlayerNotificationManager.NotificationListener and PlayerNotificationManager.CustomActionReceiver. |
| Modifier and Type | Method | Description |
|---|---|---|
protected androidx.core.app.NotificationCompat.Builder |
createNotification(Player player,
androidx.core.app.NotificationCompat.Builder builder,
boolean ongoing,
Bitmap largeIcon) |
Creates the notification given the current player state.
|
static PlayerNotificationManager |
createWithNotificationChannel(Context context,
String channelId,
int channelName,
int channelDescription,
int notificationId,
PlayerNotificationManager.MediaDescriptionAdapter mediaDescriptionAdapter) |
Creates a notification manager and a low-priority notification channel with the specified
channelId and channelName. |
static PlayerNotificationManager |
createWithNotificationChannel(Context context,
String channelId,
int channelName,
int channelDescription,
int notificationId,
PlayerNotificationManager.MediaDescriptionAdapter mediaDescriptionAdapter,
PlayerNotificationManager.NotificationListener notificationListener) |
Creates a notification manager and a low-priority notification channel with the specified
channelId and channelName. |
static PlayerNotificationManager |
createWithNotificationChannel(Context context,
String channelId,
int channelName,
int notificationId,
PlayerNotificationManager.MediaDescriptionAdapter mediaDescriptionAdapter) |
|
static PlayerNotificationManager |
createWithNotificationChannel(Context context,
String channelId,
int channelName,
int notificationId,
PlayerNotificationManager.MediaDescriptionAdapter mediaDescriptionAdapter,
PlayerNotificationManager.NotificationListener notificationListener) |
|
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 boolean |
getOngoing(Player player) |
Returns whether the generated notification should be ongoing.
|
void |
invalidate() |
Forces an update of the notification if already started.
|
void |
setBadgeIconType(int badgeIconType) |
Sets the badge icon type of the notification.
|
void |
setColor(int color) |
Sets the accent color of the notification.
|
void |
setColorized(boolean colorized) |
Sets whether the notification should be colorized.
|
void |
setControlDispatcher(ControlDispatcher controlDispatcher) |
Sets the
ControlDispatcher. |
void |
setDefaults(int defaults) |
Sets the defaults.
|
void |
setFastForwardIncrementMs(long fastForwardMs) |
Deprecated.
|
void |
setMediaSessionToken(android.support.v4.media.session.MediaSessionCompat.Token token) |
Sets the
MediaSessionCompat.Token. |
void |
setNotificationListener(PlayerNotificationManager.NotificationListener notificationListener) |
Deprecated.
Pass the notification listener to the constructor instead.
|
void |
setPlaybackPreparer(PlaybackPreparer playbackPreparer) |
Deprecated.
Use
setControlDispatcher(ControlDispatcher) instead. |
void |
setPlayer(Player player) |
Sets the
Player. |
void |
setPriority(int priority) |
Sets the priority of the notification required for API 25 and lower.
|
void |
setRewindIncrementMs(long rewindMs) |
Deprecated.
|
void |
setSmallIcon(int smallIconResourceId) |
Sets the small icon of the notification which is also shown in the system status bar.
|
void |
setUseChronometer(boolean useChronometer) |
Sets whether the elapsed time of the media playback should be displayed.
|
void |
setUseNavigationActions(boolean useNavigationActions) |
Deprecated.
|
void |
setUseNavigationActionsInCompactView(boolean useNavigationActionsInCompactView) |
Deprecated.
|
void |
setUseNextAction(boolean useNextAction) |
Sets whether the next action should be used.
|
void |
setUseNextActionInCompactView(boolean useNextActionInCompactView) |
If
useNextAction is true, sets whether the next action should
also be used in compact view. |
void |
setUsePlayPauseActions(boolean usePlayPauseActions) |
Sets whether the play and pause actions should be used.
|
void |
setUsePreviousAction(boolean usePreviousAction) |
Sets whether the previous action should be used.
|
void |
setUsePreviousActionInCompactView(boolean usePreviousActionInCompactView) |
If
usePreviousAction is true, sets whether the previous
action should also be used in compact view. |
void |
setUseStopAction(boolean useStopAction) |
Sets whether the stop action should be used.
|
void |
setVisibility(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.
|
public static final String ACTION_PLAY
public static final String ACTION_PAUSE
public static final String ACTION_PREVIOUS
public static final String ACTION_NEXT
public static final String ACTION_FAST_FORWARD
public static final String ACTION_REWIND
public static final String ACTION_STOP
public static final String EXTRA_INSTANCE_ID
public PlayerNotificationManager(Context context, String channelId, int notificationId, PlayerNotificationManager.MediaDescriptionAdapter mediaDescriptionAdapter)
channelId. The caller
is responsible for creating the notification channel.
When used within a service, consider using PlayerNotificationManager(Context,
String, int, MediaDescriptionAdapter, NotificationListener) to which a PlayerNotificationManager.NotificationListener can be passed.
context - The Context.channelId - The id of the notification channel.notificationId - The id of the notification.mediaDescriptionAdapter - The PlayerNotificationManager.MediaDescriptionAdapter.public PlayerNotificationManager(Context context, String channelId, int notificationId, PlayerNotificationManager.MediaDescriptionAdapter mediaDescriptionAdapter, @Nullable PlayerNotificationManager.NotificationListener notificationListener)
channelId and PlayerNotificationManager.NotificationListener. The caller is responsible for creating the notification channel.context - The Context.channelId - The id of the notification channel.notificationId - The id of the notification.mediaDescriptionAdapter - The PlayerNotificationManager.MediaDescriptionAdapter.notificationListener - The PlayerNotificationManager.NotificationListener.public PlayerNotificationManager(Context context, String channelId, int notificationId, PlayerNotificationManager.MediaDescriptionAdapter mediaDescriptionAdapter, @Nullable PlayerNotificationManager.CustomActionReceiver customActionReceiver)
channelId and PlayerNotificationManager.CustomActionReceiver. The caller is responsible for creating the notification channel.
When used within a service, consider using PlayerNotificationManager(Context,
String, int, MediaDescriptionAdapter, NotificationListener, CustomActionReceiver) to which a
PlayerNotificationManager.NotificationListener can be passed.
context - The Context.channelId - The id of the notification channel.notificationId - The id of the notification.mediaDescriptionAdapter - The PlayerNotificationManager.MediaDescriptionAdapter.customActionReceiver - The PlayerNotificationManager.CustomActionReceiver.public PlayerNotificationManager(Context context, String channelId, int notificationId, PlayerNotificationManager.MediaDescriptionAdapter mediaDescriptionAdapter, @Nullable PlayerNotificationManager.NotificationListener notificationListener, @Nullable PlayerNotificationManager.CustomActionReceiver customActionReceiver)
channelId, PlayerNotificationManager.NotificationListener and PlayerNotificationManager.CustomActionReceiver. The caller is responsible for creating
the notification channel.context - The Context.channelId - The id of the notification channel.notificationId - The id of the notification.mediaDescriptionAdapter - The PlayerNotificationManager.MediaDescriptionAdapter.notificationListener - The PlayerNotificationManager.NotificationListener.customActionReceiver - The PlayerNotificationManager.CustomActionReceiver.@Deprecated public static PlayerNotificationManager createWithNotificationChannel(Context context, String channelId, @StringRes int channelName, int notificationId, PlayerNotificationManager.MediaDescriptionAdapter mediaDescriptionAdapter)
public static PlayerNotificationManager createWithNotificationChannel(Context context, String channelId, @StringRes int channelName, @StringRes int channelDescription, int notificationId, PlayerNotificationManager.MediaDescriptionAdapter mediaDescriptionAdapter)
channelId and channelName.
If the player notification manager is intended to be used within a foreground service,
createWithNotificationChannel(Context, String, int, int, MediaDescriptionAdapter,
NotificationListener) should be used to which a PlayerNotificationManager.NotificationListener can be passed.
This way you'll receive the notification to put the service into the foreground by calling
Service.startForeground(int, Notification).
context - The Context.channelId - The id of the notification channel.channelName - A string resource identifier for the user visible name of the notification
channel. The recommended maximum length is 40 characters. The string may be truncated if
it's too long.channelDescription - A string resource identifier for the user visible description of the
notification channel, or 0 if no description is provided. The recommended maximum length is
300 characters. The value may be truncated if it is too long.notificationId - The id of the notification.mediaDescriptionAdapter - The PlayerNotificationManager.MediaDescriptionAdapter.@Deprecated public static PlayerNotificationManager createWithNotificationChannel(Context context, String channelId, @StringRes int channelName, int notificationId, PlayerNotificationManager.MediaDescriptionAdapter mediaDescriptionAdapter, @Nullable PlayerNotificationManager.NotificationListener notificationListener)
public static PlayerNotificationManager createWithNotificationChannel(Context context, String channelId, @StringRes int channelName, @StringRes int channelDescription, int notificationId, PlayerNotificationManager.MediaDescriptionAdapter mediaDescriptionAdapter, @Nullable PlayerNotificationManager.NotificationListener notificationListener)
channelId and channelName. The PlayerNotificationManager.NotificationListener passed as the last
parameter will be notified when the notification is created and cancelled.context - The Context.channelId - The id of the notification channel.channelName - A string resource identifier for the user visible name of the channel. The
recommended maximum length is 40 characters. The string may be truncated if it's too long.channelDescription - A string resource identifier for the user visible description of the
channel, or 0 if no description is provided.notificationId - The id of the notification.mediaDescriptionAdapter - The PlayerNotificationManager.MediaDescriptionAdapter.notificationListener - The PlayerNotificationManager.NotificationListener.public final void setPlayer(@Nullable
Player player)
Player.
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.
player - The Player to use, or null to remove the current player. Only
players which are accessed on the main thread are supported (
player.getApplicationLooper() == Looper.getMainLooper()).@Deprecated public void setPlaybackPreparer(@Nullable PlaybackPreparer playbackPreparer)
setControlDispatcher(ControlDispatcher) instead. The manager calls
ControlDispatcher.dispatchPrepare(Player) instead of PlaybackPreparer.preparePlayback(). The DefaultControlDispatcher that this manager
uses by default, calls Player.prepare(). If you wish to intercept or customize this
behaviour, you can provide a custom implementation of ControlDispatcher.dispatchPrepare(Player) and pass it to setControlDispatcher(ControlDispatcher).public final void setControlDispatcher(ControlDispatcher controlDispatcher)
ControlDispatcher.controlDispatcher - The ControlDispatcher.@Deprecated public final void setNotificationListener(PlayerNotificationManager.NotificationListener notificationListener)
PlayerNotificationManager.NotificationListener.
Please note that you should call this method before you call setPlayer(Player) or
you may not get the PlayerNotificationManager.NotificationListener.onNotificationStarted(int, Notification)
called on your listener.
notificationListener - The PlayerNotificationManager.NotificationListener.@Deprecated public final void setFastForwardIncrementMs(long fastForwardMs)
@Deprecated public final void setRewindIncrementMs(long rewindMs)
public void setUseNextAction(boolean useNextAction)
useNextAction - Whether to use the next action.public void setUsePreviousAction(boolean usePreviousAction)
usePreviousAction - Whether to use the previous action.@Deprecated public final void setUseNavigationActions(boolean useNavigationActions)
useNavigationActions - Whether to use navigation actions.public void setUseNextActionInCompactView(boolean useNextActionInCompactView)
useNextAction is true, sets whether the next action should
also be used in compact view. Has no effect if useNextAction is
false.useNextActionInCompactView - Whether to use the next action in compact view.public void setUsePreviousActionInCompactView(boolean usePreviousActionInCompactView)
usePreviousAction is true, sets whether the previous
action should also be used in compact view. Has no effect if usePreviousAction is false.usePreviousActionInCompactView - Whether to use the previous action in compact view.@Deprecated public final void setUseNavigationActionsInCompactView(boolean useNavigationActionsInCompactView)
useNavigationActions is true, sets whether
navigation actions should also be used in compact view. Has no effect if useNavigationActions is false.useNavigationActionsInCompactView - Whether to use navigation actions in compact view.public final void setUsePlayPauseActions(boolean usePlayPauseActions)
usePlayPauseActions - Whether to use play and pause actions.public final void setUseStopAction(boolean useStopAction)
useStopAction - Whether to use the stop action.public final void setMediaSessionToken(android.support.v4.media.session.MediaSessionCompat.Token token)
MediaSessionCompat.Token.token - The MediaSessionCompat.Token.public final void setBadgeIconType(int badgeIconType)
See NotificationCompat.Builder.setBadgeIconType(int).
badgeIconType - The badge icon type.public final void setColorized(boolean colorized)
setColor(int) will be used as the background color for the notification.
See NotificationCompat.Builder.setColorized(boolean).
colorized - Whether to colorize the notification.public final void setDefaults(int defaults)
See NotificationCompat.Builder.setDefaults(int).
defaults - The default notification options.public final void setColor(int color)
See NotificationCompat.Builder.setColor(int).
color - The color, in ARGB integer form like the constants in Color.public final void setPriority(@Priority int priority)
See NotificationCompat.Builder.setPriority(int).
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 the constructor.
priority - The priority which can be one of NotificationCompat.PRIORITY_DEFAULT,
NotificationCompat.PRIORITY_MAX, NotificationCompat.PRIORITY_HIGH, NotificationCompat.PRIORITY_LOW or NotificationCompat.PRIORITY_MIN. If not set
NotificationCompat.PRIORITY_LOW is used by default.public final void setSmallIcon(@DrawableRes
int smallIconResourceId)
See NotificationCompat.Builder.setSmallIcon(int).
smallIconResourceId - The resource id of the small icon.public final void setUseChronometer(boolean useChronometer)
Note that this setting only works if all of the following are true:
actively playing.
dynamically changing its
duration (like for example a live stream).
interrupted by an ad.
regular speed.
See NotificationCompat.Builder.setUsesChronometer(boolean).
useChronometer - Whether to use chronometer.public final void setVisibility(@Visibility int visibility)
See NotificationCompat.Builder.setVisibility(int).
visibility - The visibility which must be one of NotificationCompat.VISIBILITY_PUBLIC, NotificationCompat.VISIBILITY_PRIVATE or
NotificationCompat.VISIBILITY_SECRET.public void invalidate()
@Nullable protected androidx.core.app.NotificationCompat.Builder createNotification(Player player, @Nullable androidx.core.app.NotificationCompat.Builder builder, boolean ongoing, @Nullable Bitmap largeIcon)
player - The player for which state to build a notification.builder - The builder used to build the last notification, or null. Re-using the
builder when possible can prevent notification flicker when Util#SDK_INT < 21.ongoing - Whether the notification should be ongoing.largeIcon - The large icon to be used.NotificationCompat.Builder on which to call NotificationCompat.Builder.build() to obtain the notification, or null if no
notification should be displayed.protected List<String> getActions(Player player)
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 PlayerNotificationManager.CustomActionReceiver.createCustomActions(Context, int). Otherwise the
action name is ignored.
protected int[] getActionIndicesForCompactView(List<String> actionNames, Player player)
This method can be overridden. The indices must refer to the list of actions passed as the first parameter.
actionNames - The names of the actions included in the notification.player - The player for which a notification is being built.protected boolean getOngoing(Player player)