HomeAndroidMedia3 is able to play!

Media3 is able to play!



Posted by Nevin Mital – Developer Relations Engineer, Android Media

At this time, we’re happy to announce the complete launch of the Jetpack Media3 library. After sharing a primary take a look at the library at Android Developer Summit 2021, we revealed a number of alpha and beta releases over the previous a number of months to make sure a high-quality set of APIs that we now encourage everybody to undertake.

Media3 is the brand new dwelling for APIs that allow you to create wealthy audio and video experiences. When you’ve used libraries like ExoPlayer, MediaCompat, or Media2, you’ll discover Media3 to be acquainted. Nevertheless, as an alternative of utilizing these separate libraries, Media3 offers a unified API for playback use-cases and likewise expands to cowl new use-cases like video modifying and transcoding. The APIs are easy to make use of but highly effective, customizable to fulfill your wants, and dependable and optimized so you possibly can construct for the various Android machine ecosystem.

On this weblog put up, we’ll give attention to the playback APIs in Media3, so please keep tuned for an upcoming put up the place we’ll dive deeper into the video modifying and transcoding APIs. As a short introduction, the next desk describes key elements for playback in Media3:

Participant

An interface that defines conventional high-level performance for an audio or video participant, resembling playback controls.

ExoPlayer

The default implementation of the Participant interface in Media3.

MediaSession

An API that advertises media playback to and receives playback command requests from exterior purchasers.

MediaSessionService

A service that holds a MediaSession to allow background playback.

MediaLibraryService

A service that moreover means that you can expose a content material library to exterior purchasers.

MediaController

An API that’s typically utilized by exterior purchasers to retrieve playback info and ship playback command requests to your media app. Complementary to a MediaSession. Examples of exterior purchasers embody the notification and lock display media controls on cellular and huge display units, Android Auto, WearOS, and Google Assistant.

MediaBrowser

An API that moreover permits exterior purchasers to navigate your media app’s content material library. Complementary to a MediaLibraryService.

Our developer documentation has extra particulars on these elements. Let’s take a better look into what this new library affords and how one can begin utilizing it.

Protecting it easy

By consolidating the APIs for the playback developer journey right into a single library, Media3 is ready to introduce a Participant interface that’s utilized by a number of elements, resembling MediaSession and MediaController. This interface outlines conventional high-level performance for audio and video playback, resembling playback controls and the flexibility to question properties of the at the moment taking part in media.

Having a typical interface for all “player-like” elements implies that creating new cases of those objects is easy:

val participant = ExoPlayer.builder(context).construct()
val session = MediaSession.Builder(context, participant).construct()
val controller = MediaController.Builder(context, session.token).construct()

Media3’s MediaSession and MediaController will robotically mirror the state of the elements they’re related to. Because of this, you too can simplify your app’s structure by eradicating connectors like ExoPlayer’s MediaSessionConnector and extra simply observe the movement of logic via your app. Calling play() on the MediaController will ahead the motion to the MediaSession, which is able to then ahead it to the participant.

Equally, Media3 goals to make background playback circumstances simpler to deal with. The PlayerNotificationManager from ExoPlayer is now not wanted, as Media3’s MediaSessionService and MediaLibraryService robotically deal with publishing a media notification as wanted. The library handles configuring, beginning, and stopping a foreground service for you as wanted, however please additionally word some identified points summarized in this remark.

ExoPlayer is deprecated, lengthy stay ExoPlayer!

ExoPlayer has a brand new dwelling and is the default implementation of the aforementioned Participant interface in Media3. The standalone ExoPlayer mission, with package deal title com.google.android.exoplayer2, will quickly be discontinued, and future updates can be revealed in Media3. For the following few months, we’ll proceed publishing equal releases of each Media3 and ExoPlayer that will help you make the transition to Media3. For instance, which means ExoPlayer 2.18.5 and ExoPlayer in Media3 1.0.0 are similar except for their package deal names. Nevertheless, that is solely non permanent and we are going to deprecate the standalone ExoPlayer later this yr, so we extremely suggest migrating to Media3 as quickly as doable. The “Migrating to Media3” part beneath describes the method in additional element, which features a script that handles many of the be just right for you.

Notice that Media3 is developed with the identical philosophy as ExoPlayer (and in reality, is developed by the identical crew!). In different phrases, Media3 retains ExoPlayer’s customizable elements, open supply improvement on GitHub, receptivity to tug requests, and public subject tracker, to call just a few similarities.

Migrating to Media3

As talked about beforehand, the standalone ExoPlayer mission, with package deal title com.google.android.exoplayer2, will quickly be discontinued, so to proceed receiving updates, you have to emigrate to Media3 ExoPlayer. Different Media APIs that ought to be migrated to Media3 embody, however will not be restricted to, MediaSessionConnectorMediaBrowserServiceCompat, and MediaBrowserCompat.

We’ve ready two key sources that will help you obtain this migration as easily as doable:

  1. migration information to stroll you thru the method step-by-step
  2. migration script to transform your standalone ExoPlayer mission packages to the corresponding new modules and packages below Media3

The excellent news is that for those who’re at the moment utilizing ExoPlayer, there’s no want for any code adjustments and no must re-integrate or re-write any customizations. The standalone ExoPlayer and Media3 ExoPlayer are similar except for the package deal title, and the conversion could be accomplished robotically with the aforementioned migration script. Simply be sure you’ve up to date your mission to make use of the newest model of ExoPlayer earlier than getting began. For full particulars and steps, please discuss with the migration information.

Moreover, since Media3 is totally backwards-compatible with prior media APIs resembling MediaControllerCompat and MediaMetadataCompat, your present integrations will proceed to work as earlier than even after the migration. Notice that new options resembling per-controller customization of instructions are solely obtainable for purchasers utilizing Media3. That’s to say, for instance, all legacy controllers, resembling MediaControllerCompat, will obtain the identical set of accessible instructions. You possibly can determine a legacy controller by checking if getControllerVersion() returns 0 within the MediaSession.ControllerInfo.

The facility of Media3, within the palm of your hand

Media3 affords a number of choices so that you can regulate its conduct to higher suit your wants. The following few sections describe some such mechanisms.

Play it your personal manner

Though ExoPlayer is the really useful Participant implementation to make use of for audio and video streaming apps, Media3 additionally introduces the SimpleBasePlayer to reduce the variety of strategies it is advisable to implement to combine with a customized participant. Begin by implementing the getState technique. That is the place you possibly can declare the Command set supported by your participant and configure metadata such because the at the moment taking part in media merchandise index and the present timestamp.

class CustomPlayer : SimpleBasePlayer(looper) {
override enjoyable getState(): State {

}

}

The SimpleBasePlayer class will implement legitimate participant state and deal with informing listeners of state adjustments. Moreover, any strategies associated to a Command you don’t declare as obtainable are ignored, so past getState, you solely must implement the strategies that may truly be used.

Higher management over your instructions

The MediaSession and MediaController APIs have additionally been up to date to offer you extra management. With Media3, you possibly can promote your app’s playback capabilities on a per-controller foundation. Modify the instructions obtainable to a consumer app within the onConnect technique of your MediaSession.Callback. For instance, to forestall a consumer app with package deal title com.instance.myClient from gaining access to the “search to subsequent media merchandise” Participant.Command:

var sessionCallback = object : MediaSession.Callback {
override enjoyable onConnect(
session: MediaSession,
controller: MediaSession.ControllerInfo
)
: MediaSession.ConnectionResult {
val connectionResult = tremendous.onConnect(session, controller)
if (controller.packageName == "com.instance.myClient") {
val availablePlayerCommands = connectionResult.availablePlayerCommands.buildUpon()
.take away(Participant.COMMAND_SEEK_TO_NEXT_MEDIA_ITEM)
.construct()
return MediaSession.ConnectionResult.settle for(
connectionResult.availableSessionCommands,
availablePlayerCommands
)
}
return connectionResult
}
}

var mediaSession = MediaSession.Builder(context, participant)
.setCallback(sessionCallback)
.construct()


Creating customized instructions

In fact, as with the earlier media APIs, you possibly can add customized instructions tailor-made to your app. To implement a customized command, create a brand new SessionCommand. Much like as proven above, you may give controllers entry to this tradition command by together with it within the record of accessible session instructions. You possibly can deal with customized command conduct within the onCustomCommand technique of the identical Callback:

override enjoyable onCustomCommand(
session: MediaSession,
controller: MediaSession.ControllerInfo,
customCommand: SessionCommand,
args: Bundle
)
: ListenableFuture<SessionResult> {
if (customCommand.customAction == MY_CUSTOM_COMMAND) {

return Futures.immediateFuture(SessionResult(SessionResult.RESULT_SUCCESS))
}

return Futures.immediateFuture(SessionResult(SessionResult.RESULT_ERROR_BAD_VALUE))
}

You can too ask consumer apps to show your customized command by together with it in a setCustomLayout name within the onPostConnect technique of the MediaSession.Callback.

Subsequent steps

We’d love so that you can begin utilizing Media3 in your app! 

To begin exploring the library, be at liberty to take a look at the demo app to see an instance of audio and video playback, together with the way to combine with a media session. Keep tuned to our developer guides for extra detailed steerage on the totally different elements in Media3 touchdown quickly. Our pattern app, the Common Android Music Participant, and our testing instrument, the Media Controller Take a look at app, can even be up to date to Media3 on their important branches within the coming weeks.

When you run into any points, have any characteristic requests, or want to share every other kind of suggestions, please tell us utilizing the Media3 subject tracker on GitHub. We look ahead to listening to from you!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments