MediaSessionCompat

A MediaSession should be created when an app wants to publish media playback information or handle media keys. In general an app only needs one session for all playback, though multiple sessions can be created to provide finer grain controls of media.

Once a session is created the owner of the session may pass its session token to other processes to allow them to create a MediaControllerCompat to interact with the session.

ACTION_FOLLOW

Predefined custom action to follow an artist, album, or playlist. The extra bundle must have ARGUMENT_MEDIA_ATTRIBUTE to indicate the type of the follow action. The bundle can also have an optional string argument, ARGUMENT_MEDIA_ATTRIBUTE_VALUE, to specify the target to follow (e.g., the name of the artist to follow). If this argument is omitted, the currently playing media will be the target of the action. Thus, the session must perform the follow action with the current metadata. If there's no specified attribute in the current metadata, the controller must not omit this argument.

ACTION_UNFOLLOW

Predefined custom action to unfollow an artist, album, or playlist. The extra bundle must have ARGUMENT_MEDIA_ATTRIBUTE to indicate the type of the unfollow action. The bundle can also have an optional string argument, ARGUMENT_MEDIA_ATTRIBUTE_VALUE, to specify the target to unfollow (e.g., the name of the artist to unfollow). If this argument is omitted, the currently playing media will be the target of the action. Thus, the session must perform the unfollow action with the current metadata. If there's no specified attribute in the current metadata, the controller must not omit this argument.

Note: From API 24 to 27, the RemoteUserInfo returned from this method will have negative uid and pid. Most of the cases it will have the correct package name, but sometimes it will fail to get the right one.

getSessionToken

Retrieves a token object that can be used by apps to create a MediaControllerCompat for interacting with this session. The owner of the session is responsible for deciding how to distribute these tokens.

On platform versions before android.os.Build.VERSION_CODES#LOLLIPOP this token may only be used within your app as there is no way to guarantee other apps are using the same version of the support library.

isActive

release

This must be called when an app has finished performing playback. If playback is expected to start again shortly the session can be left open, but it must be released if your activity or service is being destroyed.

setActive

Sets if this session is currently active and ready to receive commands. If set to false your session's controller may not be discoverable. You must set the session to active before it can start receiving media button events or transport commands.

setExtras

Sets some extras that can be associated with the MediaSessionCompat. No assumptions should be made as to how a MediaControllerCompat will handle these extras. Keys should be fully qualified (e.g. com.example.MY_EXTRA) to avoid conflicts.

setFlags

setMediaButtonReceiver

Sets a pending intent for your media button receiver to allow restarting playback after the session has been stopped. If your app is started in this way an Intent#ACTION_MEDIA_BUTTON intent will be sent via the pending intent.

setPlaybackToLocal

Sets the stream this session is playing on. This will affect the system's volume handling for this session. If setPlaybackToRemote was previously called it will stop receiving volume commands and the system will begin sending volume changes to the appropriate stream.

setPlaybackToRemote

Configures this session to use remote volume handling. This must be called to receive volume button events, otherwise the system will adjust the current stream volume for this session. If setPlaybackToLocal was previously called that stream will stop receiving volume changes for this session.

setSessionActivity

Sets an intent for launching UI for this Session. This can be used as a quick link to an ongoing media screen. The intent should be for an activity that may be started using Activity#startActivity(Intent).