java.lang.Object | |
↳ | android.media.session.MediaSession |
Allows interaction with media controllers, volume keys, media buttons, and transport controls.
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.
A MediaSession is created by calling
createSession(String)
. Once a session is created
the owner of the session may use getSessionToken()
to allow apps to
create a MediaController
to interact with this session.
To receive commands, media keys, and other events a MediaSession.Callback
must be
set with addCallback(Callback)
. To receive transport control
commands a MediaSession.TransportControlsCallback
must be set with
addTransportControlsCallback(MediaSession.TransportControlsCallback)
.
When an app is finished performing playback it must call release()
to clean up the session and notify any controllers.
MediaSession objects are thread safe
Nested Classes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
MediaSession.Callback | Receives generic commands or updates from controllers and the system. | ||||||||||
MediaSession.TransportControlsCallback | Receives transport control commands. |
Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
int | FLAG_HANDLES_MEDIA_BUTTONS | Set this flag on the session to indicate that it can handle media button events. | |||||||||
int | FLAG_HANDLES_TRANSPORT_CONTROLS | Set this flag on the session to indicate that it handles transport
control commands through a MediaSession.TransportControlsCallback . |
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Add a callback to receive updates on for the MediaSession.
| |||||||||||
Add a callback to receive updates for the MediaSession.
| |||||||||||
Add a callback to receive transport controls on, such as play, rewind, or
fast forward.
| |||||||||||
Add a callback to receive transport controls on, such as play, rewind, or
fast forward.
| |||||||||||
Retrieve a token object that can be used by apps to create a
MediaController for interacting with this session. | |||||||||||
Get the current active state of this session.
| |||||||||||
This must be called when an app has finished performing playback.
| |||||||||||
Remove a callback.
| |||||||||||
Stop receiving transport controls on the specified callback.
| |||||||||||
Send a proprietary event to all MediaControllers listening to this
Session.
| |||||||||||
Set if this session is currently active and ready to receive commands.
| |||||||||||
Set any flags for the session.
| |||||||||||
Set an intent for launching UI for this Session.
| |||||||||||
Update the current metadata.
| |||||||||||
Update the current playback state.
| |||||||||||
Set the stream this session is playing on.
| |||||||||||
Configure this session to use remote volume handling.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.lang.Object
|
Set this flag on the session to indicate that it can handle media button events.
Set this flag on the session to indicate that it handles transport
control commands through a MediaSession.TransportControlsCallback
. The
callback can be retrieved by calling
addTransportControlsCallback(MediaSession.TransportControlsCallback)
.
Add a callback to receive updates on for the MediaSession. This includes media button and volume events. The caller's thread will be used to post events.
callback | The callback object |
---|
Add a callback to receive updates for the MediaSession. This includes media button and volume events.
callback | The callback to receive updates on. |
---|---|
handler | The handler that events should be posted on. |
Add a callback to receive transport controls on, such as play, rewind, or fast forward.
callback | The callback object |
---|
Add a callback to receive transport controls on, such as play, rewind, or fast forward. The updates will be posted to the specified handler. If no handler is provided they will be posted to the caller's thread.
callback | The callback to receive updates on |
---|---|
handler | The handler to post the updates on |
Retrieve a token object that can be used by apps to create a
MediaController
for interacting with this session. The owner of
the session is responsible for deciding how to distribute these tokens.
Get the current active state of this session.
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.
Remove a callback. It will no longer receive updates.
callback | The callback to remove. |
---|
Stop receiving transport controls on the specified callback. If an update has already been posted you may still receive it after this call returns.
callback | The callback to stop receiving updates on |
---|
Send a proprietary event to all MediaControllers listening to this Session. It's up to the Controller/Session owner to determine the meaning of any events.
event | The name of the event to send |
---|---|
extras | Any extras included with the event |
Set 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.
active | Whether this session is active or not. |
---|
Set any flags for the session.
flags | The flags to set for this session. |
---|
Set an intent for launching UI for this Session. This can be used as a quick link to an ongoing media screen.
pi | The intent to launch to show UI for this Session. |
---|
Update the current metadata. New metadata can be created using
MediaMetadata.Builder
.
metadata | The new metadata |
---|
Update the current playback state.
state | The current state of playback |
---|
Set the stream this session is playing on. This will affect the system's
volume handling for this session. If setPlaybackToRemote(RemoteVolumeProvider)
was
previously called it will stop receiving volume commands and the system
will begin sending volume changes to the appropriate stream.
By default sessions are on STREAM_MUSIC
.
stream | The AudioManager stream this session is playing on.
|
---|
Configure 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(int)
was previously called that stream will stop receiving volume changes for
this session.
volumeProvider | The provider that will handle volume changes. May not be null. |
---|