
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 have a look at the library at Android Developer Summit 2021, we printed 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. For those who’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 supplies a unified API for playback use-cases and likewise expands to cowl new use-cases like video enhancing 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 gadget ecosystem.
On this weblog submit, we’ll concentrate on the playback APIs in Media3, so please keep tuned for an upcoming submit the place we’ll dive deeper into the video enhancing and transcoding APIs. As a quick introduction, the next desk describes key parts for playback in Media3:
Our developer documentation has extra particulars on these parts. Let’s take a more in-depth 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 parts, equivalent to MediaSession and MediaController. This interface outlines conventional high-level performance for audio and video playback, equivalent to playback controls and the flexibility to question properties of the at present taking part in media.
Having a standard interface for all “player-like” parts signifies that creating new cases of those objects is simple:
val participant = ExoPlayer.builder(context).construct() |
Media3’s MediaSession and MediaController will routinely replicate the state of the parts they’re linked to. Consequently, you may also simplify your app’s structure by eradicating connectors like ExoPlayer’s MediaSessionConnector and extra simply comply with the stream of logic by 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 instances simpler to deal with. The PlayerNotificationManager from ExoPlayer is not wanted, as Media3’s MediaSessionService and MediaLibraryService routinely 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 notice some recognized points summarized in this remark.
ExoPlayer is deprecated, lengthy dwell ExoPlayer!
ExoPlayer has a brand new dwelling and is the default implementation of the aforementioned Participant interface in Media3. The standalone ExoPlayer challenge, with package deal title com.google.android.exoplayer2, will quickly be discontinued, and future updates shall be printed in Media3. For the following few months, we’ll proceed publishing equal releases of each Media3 and ExoPlayer that can assist you make the transition to Media3. For instance, because of this ExoPlayer 2.18.5 and ExoPlayer in Media3 1.0.0 are equivalent except for their package deal names. Nevertheless, that is solely short-term and we’ll deprecate the standalone ExoPlayer later this yr, so we extremely suggest migrating to Media3 as quickly as attainable. The “Migrating to Media3” part under describes the method in additional element, which features a script that handles a lot 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 workforce!). In different phrases, Media3 retains ExoPlayer’s customizable parts, open supply improvement on GitHub, receptivity to tug requests, and public difficulty tracker, to call just a few similarities.
Migrating to Media3
As talked about beforehand, the standalone ExoPlayer challenge, with package deal title com.google.android.exoplayer2, will quickly be discontinued, so to proceed receiving updates, you will want emigrate to Media3 ExoPlayer. Different Media APIs that ought to be migrated to Media3 embody, however should not restricted to, MediaSessionConnector, MediaBrowserServiceCompat, and MediaBrowserCompat.
We’ve ready two key sources that can assist you obtain this migration as easily as attainable:
- A migration information to stroll you thru the method step-by-step
- A migration script to transform your standalone ExoPlayer challenge packages to the corresponding new modules and packages below Media3
The excellent news is that in case you’re at present utilizing ExoPlayer, there’s no want for any code adjustments and no have to re-integrate or re-write any customizations. The standalone ExoPlayer and Media3 ExoPlayer are equivalent except for the package deal title, and the conversion may be performed routinely with the aforementioned migration script. Simply ensure you’ve up to date your challenge 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 absolutely backwards-compatible with prior media APIs equivalent to MediaControllerCompat and MediaMetadataCompat, your present integrations will proceed to work as earlier than even after the migration. Notice that new options equivalent to per-controller customization of instructions are solely accessible for shoppers utilizing Media3. That’s to say, for instance, all legacy controllers, equivalent to MediaControllerCompat, will obtain the identical set of accessible instructions. You’ll be able to determine a legacy controller by checking if getControllerVersion() returns 0 within the MediaSession.ControllerInfo.
The ability of Media3, within the palm of your hand
Media3 affords a number of choices so that you can alter its conduct to raised suit your wants. The subsequent few sections describe some such mechanisms.
Play it your individual means
Though ExoPlayer is the beneficial Participant implementation to make use of for audio and video streaming apps, Media3 additionally introduces the SimpleBasePlayer to reduce the variety of strategies it’s worthwhile to implement to combine with a customized participant. Begin by implementing the getState methodology. That is the place you possibly can declare the Command set supported by your participant and configure metadata such because the at present taking part in media merchandise index and the present timestamp.
class CustomPlayer : SimpleBasePlayer(looper) { |
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 accessible are ignored, so past getState, you solely have to 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 provide you extra management. With Media3, you possibly can promote your app’s playback capabilities on a per-controller foundation. Modify the instructions accessible to a shopper app within the onConnect methodology of your MediaSession.Callback. For instance, to stop a shopper app with package deal title com.instance.myClient from accessing the “search to subsequent media merchandise” Participant.Command:
var sessionCallback = object : MediaSession.Callback { |
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. Just like as proven above, you may give controllers entry to this tradition command by together with it within the checklist of accessible session instructions. You’ll be able to deal with customized command conduct within the onCustomCommand methodology of the identical Callback:
override enjoyable onCustomCommand( |
You can even ask shopper apps to show your customized command by together with it in a setCustomLayout name within the onPostConnect methodology 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 happy to take a look at the demo app to see an instance of audio and video playback, together with learn how to combine with a media session. Keep tuned to our developer guides for extra detailed steerage on the completely different parts in Media3 touchdown quickly. Our pattern app, the Common Android Music Participant, and our testing device, the Media Controller Take a look at app, can even be up to date to Media3 on their principal branches within the coming weeks.
For those who run into any points, have any characteristic requests, or want to share another type of suggestions, please tell us utilizing the Media3 difficulty tracker on GitHub. We look ahead to listening to from you!