Media3 is able to play! | Digital Noch


Media3 is able to play! | Digital Noch

Posted by Nevin Mital – Developer Relations Engineer, Android Media
Media3 is able to play! | Digital Noch Digital Noch

Right this moment, we’re happy to announce the total 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 residence for APIs that allow you to create wealthy audio and video experiences. In case 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 satisfy your wants, and dependable and optimized so you’ll be able to construct for the varied Android system ecosystem.

On this weblog publish, we’ll give attention to the playback APIs in Media3, so please keep tuned for an upcoming publish 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 usually utilized by exterior purchasers to retrieve playback data and ship playback command requests to your media app. Complementary to a MediaSession. Examples of exterior purchasers embrace the notification and lock display screen media controls on cell and enormous display screen gadgets, 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 more in-depth look into what this new library provides and how one can begin utilizing it.

Retaining 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 present taking part in media.

Having a standard interface for all “player-like” elements implies that creating new situations 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 mechanically mirror the state of the elements they’re related to. Because of this, you can too simplify your app’s structure by eradicating connectors like ExoPlayer’s MediaSessionConnector and extra simply observe the movement of logic by your app. Calling play() on the MediaController will ahead the motion to the MediaSession, which can 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 mechanically 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 observe some recognized points summarized in this remark.

ExoPlayer is deprecated, lengthy reside ExoPlayer!

ExoPlayer has a brand new residence and is the default implementation of the aforementioned Participant interface in Media3. The standalone ExoPlayer venture, with package deal identify com.google.android.exoplayer2, will quickly be discontinued, and future updates might 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’ll deprecate the standalone ExoPlayer later this yr, so we extremely suggest migrating to Media3 as quickly as potential. 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.

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

Migrating to Media3

As talked about beforehand, the standalone ExoPlayer venture, with package deal identify com.google.android.exoplayer2, will quickly be discontinued, so to proceed receiving updates, you’ll need emigrate to Media3 ExoPlayer. Different Media APIs that ought to be migrated to Media3 embrace, however should not restricted to, MediaSessionConnectorMediaBrowserServiceCompat, and MediaBrowserCompat.

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

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

The excellent news is that when 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 similar except for the package deal identify, and the conversion will be finished mechanically with the aforementioned migration script. Simply be sure to’ve up to date your venture to make use of the newest model of ExoPlayer earlier than getting began. For full particulars and steps, please check with the migration information.

Moreover, since Media3 is absolutely backwards-compatible with prior media APIs resembling MediaControllerCompat and MediaMetadataCompat, your current integrations will proceed to work as earlier than even after the migration. Observe 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’ll be able to establish a legacy controller by checking if getControllerVersion() returns 0 within the MediaSession.ControllerInfo.

The ability of Media3, within the palm of your hand

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

Play it your personal approach

Though ExoPlayer is the really helpful Participant implementation to make use of for audio and video streaming apps, Media3 additionally introduces the SimpleBasePlayer to reduce the variety of strategies you have to implement to combine with a customized participant. Begin by implementing the getState technique. That is the place you’ll be able to 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) {
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 have to implement the strategies that can truly be used.

Higher management over your instructions

The MediaSession and MediaController APIs have additionally been up to date to present you extra management. With Media3, you’ll be able to 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 identify com.instance.myClient from getting 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

After all, as with the earlier media APIs, you’ll be able to 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 checklist of accessible session instructions. You’ll be able to 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))
}

It’s also possible to 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 methods 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 device, the Media Controller Take a look at app, will even be up to date to Media3 on their most important branches within the coming weeks.

In case you run into any points, have any function requests, or want to share another kind of suggestions, please tell us utilizing the Media3 subject tracker on GitHub. We stay up for listening to from you!

Related articles

spot_img

Leave a reply

Please enter your comment!
Please enter your name here