Media Controls | Android Developers

Android 11 updates how media controls are displayed, making use of theMediaSession and MediaRouter2 APIs to render controls and audio outputinformation.

Media controls in Android 11 are located near the Quick Settings. Sessions frommultiple apps are arranged in a swipeable carousel. The carousel listssessions in this order:

Users can restart previous sessions from the carousel without having to startthe app. When playback begins, the user interacts with the media controls in theusual way.

In order to use this feature, you mustenable Media resumption in the Developer Options settings.

To make your player app appear in the quick setting settings area,you must create a MediaStyle notification with a valid MediaSession token.

To display the brand icon for the media player, useNotificationBuilder.setSmallIcon().

To support playback resumption, apps must implement a MediaBrowserServiceand MediaSession.

The playback resumption feature can be be turned off using the Settings app,under the Sound > Media options. The user can also access Settings bytapping the gear icon that appears after swiping on the expanded carousel.

After the device boots, the system looks for the five most recently used mediaapps, and provides controls that can be used to restart playing from each app.

The system attempts to contact your MediaBrowserService with a connection fromSystemUI. Your app must allow such connections, otherwise it cannot supportplayback resumption.

Connections from SystemUI can be identified and verified using the package namecom.android.systemui and signature. The SystemUI is signed with the platformsignature. An example of how to check against the platform signature can befound in the UAMP app.

In order to support playback resumption, your MediaBrowserService mustimplement these behaviors:

onGetRoot() must return a non-null root quickly. Other complex logic shouldbe handled in onLoadChildren()

WhenonLoadChildren() is called on the root media ID, the result must contain aFLAG_PLAYABLEchild.

MediaBrowserService should return the most recently played media item whenthey receive anEXTRA_RECENTquery. The value returned should be an actual media item rather than genericfunction.

MediaBrowserService must provide an appropriateMediaDescription with a non-emptytitle andsubtitle.It should also set anicon URIor anicon bitmap.

The following code examples illustrate how to implement onGetRoot().

The system retrieves the following information from theMediaSession's MediaMetadata, and displays it when it is available:

In order to support play resumption, your MediaSession must implement aMediaSession callback for onPlay().

The media player shows the elapsed time for the currently playingmedia, along with a seek bar which is mapped to the MediaSessionPlaybackState.

In order for the the seek bar to work correctly, you must implementPlaybackState.Builder#setActions and include ACTION_SEEK_TO. Otherwise theplayer only shows the elapsed time and duration.

To set the player controls, use Notification.Builder#setCustomActions. Onlythe actions indicated withNotification.MediaStyle#setShowsActionsInCompactView willbe displayed in the media player appearing in the collapsed quick settings.

Read the original:
Media Controls | Android Developers

Related Posts

Comments are closed.