public class PlayerNotificationManager
extends java.lang.Object
The notification is cancelled when null
is passed to setPlayer(Player)
or
when an intent with action ACTION_STOP
is received.
If the player is released it must be removed from the manager by calling setPlayer(null)
which will cancel the notification.
useNavigationActions
- Sets whether the navigation previous and next actions
are displayed.
setUseNavigationActions(boolean)
stopAction
- Sets which stop action should be used. If set to null, the stop
action is not displayed.
setStopAction(String)
}
rewindIncrementMs
- Sets the rewind increment. If set to zero the rewind
action is not displayed.
setRewindIncrementMs(long)
DEFAULT_REWIND_MS
(5000)
fastForwardIncrementMs
- Sets the fast forward increment. If set to zero the
fast forward action is not included in the notification.
setFastForwardIncrementMs(long)
}
DEFAULT_FAST_FORWARD_MS
(5000)
Modifier and Type | Class and 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 start and cancellation of 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 and Description |
---|---|
static java.lang.String |
ACTION_FAST_FORWARD
The action which fast forwards.
|
static java.lang.String |
ACTION_NEXT
The action which skips to the next window.
|
static java.lang.String |
ACTION_PAUSE
The action which pauses playback.
|
static java.lang.String |
ACTION_PLAY
The action which starts playback.
|
static java.lang.String |
ACTION_PREVIOUS
The action which skips to the previous window.
|
static java.lang.String |
ACTION_REWIND
The action which rewinds.
|
static java.lang.String |
ACTION_STOP
The action which cancels the notification and stops playback.
|
static int |
DEFAULT_FAST_FORWARD_MS
The default fast forward increment, in milliseconds.
|
static int |
DEFAULT_REWIND_MS
The default rewind increment, in milliseconds.
|
static java.lang.String |
EXTRA_INSTANCE_ID
The extra key of the instance id of the player notification manager.
|
Constructor and Description |
---|
PlayerNotificationManager(android.content.Context context,
java.lang.String channelId,
int notificationId,
PlayerNotificationManager.MediaDescriptionAdapter mediaDescriptionAdapter)
Creates a notification manager using the specified notification
channelId . |
PlayerNotificationManager(android.content.Context context,
java.lang.String channelId,
int notificationId,
PlayerNotificationManager.MediaDescriptionAdapter mediaDescriptionAdapter,
PlayerNotificationManager.CustomActionReceiver customActionReceiver)
Creates a notification manager using the specified notification
channelId and PlayerNotificationManager.CustomActionReceiver . |
Modifier and Type | Method and Description |
---|---|
protected android.app.Notification |
createNotification(com.google.android.exoplayer2.Player player,
android.graphics.Bitmap largeIcon)
Creates the notification given the current player state.
|
static PlayerNotificationManager |
createWithNotificationChannel(android.content.Context context,
java.lang.String channelId,
int channelName,
int notificationId,
PlayerNotificationManager.MediaDescriptionAdapter mediaDescriptionAdapter)
Creates a notification manager and a low-priority notification channel with the specified
channelId and channelName . |
protected int[] |
getActionIndicesForCompactView(java.util.List<java.lang.String> actionNames,
com.google.android.exoplayer2.Player player)
Gets an array with the indices of the buttons to be shown in compact mode.
|
protected java.util.List<java.lang.String> |
getActions(com.google.android.exoplayer2.Player player)
Gets the names and order of the actions to be included in the notification at the current
player state.
|
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(com.google.android.exoplayer2.ControlDispatcher controlDispatcher)
Sets the
ControlDispatcher . |
void |
setDefaults(int defaults)
Sets the defaults.
|
void |
setFastForwardIncrementMs(long fastForwardMs)
Sets the fast forward increment in milliseconds.
|
void |
setMediaSessionToken(android.support.v4.media.session.MediaSessionCompat.Token token)
Sets the
MediaSessionCompat.Token . |
void |
setNotificationListener(PlayerNotificationManager.NotificationListener notificationListener)
|
void |
setOngoing(boolean ongoing)
Sets whether the notification should be ongoing.
|
void |
setPlayer(com.google.android.exoplayer2.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)
Sets the rewind increment in milliseconds.
|
void |
setSmallIcon(int smallIconResourceId)
Sets the small icon of the notification which is also shown in the system status bar.
|
void |
setStopAction(java.lang.String stopAction)
Sets the name of the action to be used as stop action to cancel the notification.
|
void |
setUseChronometer(boolean useChronometer)
Sets whether the elapsed time of the media playback should be displayed
|
void |
setUseNavigationActions(boolean useNavigationActions)
Sets whether the navigation actions should be used.
|
void |
setUsePlayPauseActions(boolean usePlayPauseActions)
Sets whether the play and pause actions 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 java.lang.String ACTION_PLAY
public static final java.lang.String ACTION_PAUSE
public static final java.lang.String ACTION_PREVIOUS
public static final java.lang.String ACTION_NEXT
public static final java.lang.String ACTION_FAST_FORWARD
public static final java.lang.String ACTION_REWIND
public static final java.lang.String ACTION_STOP
public static final java.lang.String EXTRA_INSTANCE_ID
public static final int DEFAULT_FAST_FORWARD_MS
public static final int DEFAULT_REWIND_MS
public PlayerNotificationManager(android.content.Context context, java.lang.String channelId, int notificationId, PlayerNotificationManager.MediaDescriptionAdapter mediaDescriptionAdapter)
channelId
. 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
.public PlayerNotificationManager(android.content.Context context, java.lang.String channelId, int notificationId, PlayerNotificationManager.MediaDescriptionAdapter mediaDescriptionAdapter, @Nullable PlayerNotificationManager.CustomActionReceiver customActionReceiver)
channelId
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
.customActionReceiver
- The PlayerNotificationManager.CustomActionReceiver
.public static PlayerNotificationManager createWithNotificationChannel(android.content.Context context, java.lang.String channelId, @StringRes int channelName, int notificationId, PlayerNotificationManager.MediaDescriptionAdapter mediaDescriptionAdapter)
channelId
and channelName
.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 value may be truncated if it is too long.notificationId
- The id of the notification.mediaDescriptionAdapter
- The PlayerNotificationManager.MediaDescriptionAdapter
.public final void setPlayer(@Nullable com.google.android.exoplayer2.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()
).public final void setControlDispatcher(com.google.android.exoplayer2.ControlDispatcher controlDispatcher)
ControlDispatcher
.controlDispatcher
- The ControlDispatcher
, or null to use DefaultControlDispatcher
.public final void setNotificationListener(PlayerNotificationManager.NotificationListener notificationListener)
notificationListener
- The PlayerNotificationManager.NotificationListener
.public final void setFastForwardIncrementMs(long fastForwardMs)
fastForwardMs
- The fast forward increment in milliseconds. A value of zero will cause the
fast forward action to be disabled.public final void setRewindIncrementMs(long rewindMs)
rewindMs
- The rewind increment in milliseconds. A value of zero will cause the rewind
action to be disabled.public final void setUseNavigationActions(boolean useNavigationActions)
useNavigationActions
- Whether to use navigation actions or not.public final void setUsePlayPauseActions(boolean usePlayPauseActions)
usePlayPauseActions
- Whether to use play and pause actions.public final void setStopAction(@Nullable java.lang.String stopAction)
null
is passed the stop action is not displayed.stopAction
- The name of the stop action which must be ACTION_STOP
or an action
provided by the PlayerNotificationManager.CustomActionReceiver
. null
to omit 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 setOngoing(boolean ongoing)
false
the user can dismiss the
notification by swiping. If in addition the stop action is enabled dismissing the notification
triggers the stop action.
See NotificationCompat.Builder#setOngoing(boolean)
.
ongoing
- Whether true
the notification is ongoing and not dismissible.public final void setPriority(@PlayerNotificationManager.Priority int priority)
See NotificationCompat.Builder#setPriority(int)
.
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)
See NotificationCompat.Builder#setUsesChronometer(boolean)
.
useChronometer
- Whether to use chronometer.public final void setVisibility(@PlayerNotificationManager.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()
protected android.app.Notification createNotification(com.google.android.exoplayer2.Player player, @Nullable android.graphics.Bitmap largeIcon)
player
- The player for which state to build a notification.largeIcon
- The large icon to be used.Notification
which has been built.protected java.util.List<java.lang.String> getActions(com.google.android.exoplayer2.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(java.util.List<java.lang.String> actionNames, com.google.android.exoplayer2.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 state to build a notification.