Class 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 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).

    Action customization

    Playback actions can be included or omitted as follows:

    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 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.

    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 the PlayerNotificationManager.MediaDescriptionAdapter passed to Builder(Context, int, String, MediaDescriptionAdapter).

    • Method Detail

      • setPlayer

        public final void setPlayer​(@Nullable
                                    Player player)
        Sets the 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()).
      • setUseNextAction

        public void setUseNextAction​(boolean useNextAction)
        Sets whether the next action should be used.
        useNextAction - Whether to use the next action.
      • setUsePreviousAction

        public void setUsePreviousAction​(boolean usePreviousAction)
        Sets whether the previous action should be used.
        usePreviousAction - Whether to use the previous action.
      • setUseNextActionInCompactView

        public 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.
        useNextActionInCompactView - Whether to use the next action in compact view.
      • setUsePreviousActionInCompactView

        public 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.
        usePreviousActionInCompactView - Whether to use the previous action in compact view.
      • setUsePlayPauseActions

        public final void setUsePlayPauseActions​(boolean usePlayPauseActions)
        Sets whether the play and pause actions should be used.
        usePlayPauseActions - Whether to use play and pause actions.
      • setUseStopAction

        public final void setUseStopAction​(boolean useStopAction)
        Sets whether the stop action should be used.
        useStopAction - Whether to use the stop action.
      • setMediaSessionToken

        public final void setMediaSessionToken​( token)
        Sets the MediaSessionCompat.Token.
        token - The MediaSessionCompat.Token.
      • setBadgeIconType

        public final void setBadgeIconType​(int badgeIconType)
        Sets the badge icon type of the notification.

        See NotificationCompat.Builder.setBadgeIconType(int).

        badgeIconType - The badge icon type.
      • setColorized

        public final void setColorized​(boolean colorized)
        Sets whether the notification should be colorized. When set, the color set with setColor(int) will be used as the background color for the notification.

        See NotificationCompat.Builder.setColorized(boolean).

        colorized - Whether to colorize the notification.
      • setDefaults

        public final void setDefaults​(int defaults)
        Sets the defaults.

        See NotificationCompat.Builder.setDefaults(int).

        defaults - The default notification options.
      • setColor

        public final void setColor​(int color)
        Sets the accent color of the notification.

        See NotificationCompat.Builder.setColor(int).

        color - The color, in ARGB integer form like the constants in Color.
      • 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 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.
      • 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).

        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:

        See NotificationCompat.Builder.setUsesChronometer(boolean).

        useChronometer - Whether to use chronometer.
      • 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).

        visibility - The visibility which must be one of NotificationCompat.VISIBILITY_PUBLIC, NotificationCompat.VISIBILITY_PRIVATE or NotificationCompat.VISIBILITY_SECRET.
      • invalidate

        public void invalidate()
        Forces an update of the notification if already started.
      • createNotification

        protected createNotification​(Player player,
                                                                                  boolean ongoing,
                                                                                  Bitmap largeIcon)
        Creates the notification given the current player state.
        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.
        The NotificationCompat.Builder on which to call to obtain the notification, or null if no notification should be displayed.
      • 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.

        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.