MediaSession.Builder
public final class MediaSession.Builder
A builder for MediaSession.
Any incoming requests from the MediaController will be handled on the application thread of the underlying Player.
Summary
Public constructors |
|---|
|
Creates a builder for |
Protected methods |
|
|---|---|
final void |
@EnsuresNonNull(value = "bitmapLoader")Updates bitmap loader to ensure its using the maximum media session size limit. |
Public constructors
Builder
public Builder(Context context, Player player)
Creates a builder for MediaSession.
| Parameters | |
|---|---|
Context context |
The context. |
Player player |
The underlying player to perform playback and handle player commands. |
| Throws | |
|---|---|
java.lang.IllegalArgumentException |
if |
Public methods
build
public MediaSession build()
Builds a MediaSession.
| Returns | |
|---|---|
MediaSession |
A new session. |
| Throws | |
|---|---|
java.lang.IllegalStateException |
if a |
setBitmapLoader
@UnstableApi
public MediaSession.Builder setBitmapLoader(BitmapLoader bitmapLoader)
Sets a BitmapLoader for the MediaSession to decode bitmaps from compressed binary data or load bitmaps from Uri.
The provided instance will likely be called repeatedly with the same request, so it would be best if any provided instance does some caching. Simple caching can be added to any BitmapLoader implementation by wrapping it in CacheBitmapLoader before passing it to this method.
If no instance is set, a CacheBitmapLoader with a DataSourceBitmapLoader inside will be used.
| Parameters | |
|---|---|
BitmapLoader bitmapLoader |
The bitmap loader |
| Returns | |
|---|---|
MediaSession.Builder |
The builder to allow chaining. |
setCallback
public MediaSession.Builder setCallback(MediaSession.Callback callback)
Sets a callback for the MediaSession to handle incoming requests from {link MediaController}.
Apps that want to allow controllers to set or add media items to the playlist, must use a callback and override its onSetMediaItems or onSetMediaItems methods.
| Parameters | |
|---|---|
MediaSession.Callback callback |
The callback. |
| Returns | |
|---|---|
MediaSession.Builder |
The builder to allow chaining. |
setCommandButtonsForMediaItems
@UnstableApi
public MediaSession.Builder setCommandButtonsForMediaItems(List<CommandButton> commandButtons)
Sets command buttons that can be added as supported media item commands.
| Parameters | |
|---|---|
List<CommandButton> commandButtons |
The command buttons. |
setCustomLayout
@CanIgnoreReturnValue
@UnstableApi
public MediaSession.Builder setCustomLayout(List<CommandButton> customLayout)
Sets the custom layout of the session.
This method will be deprecated, prefer to use setMediaButtonPreferences. Note that the media button preferences use slots to define the allowed button placement.
The button are converted to custom actions in the platform media session playback state for platform or legacy android.support.v4.media.session.MediaControllerCompat controllers (see
PlaybackStateCompat.Builder#addCustomAction(PlaybackStateCompat.CustomAction)). When converting, the custom extras of the session command is used for the extras of the platform custom action.
Controllers that connect have the custom layout of the session available with the initial connection result by default. A custom layout specific to a controller can be set when the controller connects by using an ConnectionResult.AcceptedResultBuilder.
Use MediaSession.setCustomLayout(..) to update the custom layout during the life time of the session.
On the controller side, the enabled flag is set to false if the available commands of a controller do not allow to use a button.
| Parameters | |
|---|---|
List<CommandButton> customLayout |
The ordered list of |
| Returns | |
|---|---|
MediaSession.Builder |
The builder to allow chaining. |
setExperimentalSetUseLegacySurfaceHandling
@CanIgnoreReturnValue
@UnstableApi
public MediaSession.Builder setExperimentalSetUseLegacySurfaceHandling(
boolean useLegacySurfaceHandling
)
Sets whether to use the legacy surface handling logic by ignoring the surface size.
When set to true, the session will call setVideoSurface directly, which may not work correctly with video effects. This is intended as a temporary opt-out for applications that experience issues with the new SurfaceHolder-based approach.
This method is experimental and may be removed in a future release.
| Parameters | |
|---|---|
boolean useLegacySurfaceHandling |
Whether to use the legacy surface handling logic. |
| Returns | |
|---|---|
MediaSession.Builder |
This builder. |
setExtras
public MediaSession.Builder setExtras(Bundle tokenExtras)
Sets an extras Bundle for the session token. If not set, EMPTY is used.
A controller has access to these extras through the connected token.
| Returns | |
|---|---|
MediaSession.Builder |
The builder to allow chaining. |
setId
public MediaSession.Builder setId(String id)
Sets an ID of the MediaSession. If not set, an empty string will be used.
Use this if and only if your app supports multiple playback at the same time and also wants to provide external apps to have finer-grained controls.
| Returns | |
|---|---|
MediaSession.Builder |
The builder to allow chaining. |
setMediaButtonPreferences
@CanIgnoreReturnValue
@UnstableApi
public MediaSession.Builder setMediaButtonPreferences(List<CommandButton> mediaButtonPreferences)
Sets the media button preferences.
The button are converted to custom actions in the platform media session playback state for platform or legacy android.support.v4.media.session.MediaControllerCompat controllers (see
PlaybackStateCompat.Builder#addCustomAction(PlaybackStateCompat.CustomAction)). When converting, the custom extras of the session command is used for the extras of the platform custom action.
Controllers that connect have the media button preferences of the session available with the initial connection result by default. Media button preferences specific to a controller can be set when the controller connects by using an ConnectionResult.AcceptedResultBuilder.
Use MediaSession.setMediaButtonPreferences(..) to update the media button preferences during the life time of the session.
On the controller side, the enabled flag is set to false if the available commands of a controller do not allow to use a button.
| Parameters | |
|---|---|
List<CommandButton> mediaButtonPreferences |
The ordered list of |
| Returns | |
|---|---|
MediaSession.Builder |
The builder to allow chaining. |
setPeriodicPositionUpdateEnabled
@UnstableApi
public MediaSession.Builder setPeriodicPositionUpdateEnabled(boolean isEnabled)
Sets whether periodic position updates should be sent to controllers while playing. If false, no periodic position updates are sent to controllers.
The default is true.
| Parameters | |
|---|---|
boolean isEnabled |
Whether periodic position update is enabled. |
setSessionActivity
public MediaSession.Builder setSessionActivity(PendingIntent pendingIntent)
Sets a PendingIntent to launch an android.app.Activity for the MediaSession. This can be used as a quick link to an ongoing media screen.
A client can use this pending intent to start an activity belonging to this session. On API levels below 33 the pending intent can be used as the content intent. Tapping the notification will then send that pending intent and open the activity (see 'Start an Activity from a Notification'). For API levels starting with 33, the media notification reads the pending intent directly from the session.
| Parameters | |
|---|---|
PendingIntent pendingIntent |
The pending intent. |
| Returns | |
|---|---|
MediaSession.Builder |
The builder to allow chaining. |
setSessionExtras
public MediaSession.Builder setSessionExtras(Bundle sessionExtras)
Sets the session extras. If not set, EMPTY is used.
A controller has access to session extras through getSessionExtras.
| Returns | |
|---|---|
MediaSession.Builder |
The builder to allow chaining. |
setShowPlayButtonIfPlaybackIsSuppressed
@UnstableApi
public MediaSession.Builder setShowPlayButtonIfPlaybackIsSuppressed(
boolean showPlayButtonIfPlaybackIsSuppressed
)
Sets whether a play button is shown if playback is suppressed.
The default is true.
| Parameters | |
|---|---|
boolean showPlayButtonIfPlaybackIsSuppressed |
Whether to show a play button if playback is |
Protected methods
ensureBitmapLoaderIsSizeLimited
@EnsuresNonNull(value = "bitmapLoader")
protected final void ensureBitmapLoaderIsSizeLimited()
Updates bitmap loader to ensure its using the maximum media session size limit.