about MAD about Media. Trendy Android Improvement (MAD) for… | by Android Builders | Android Builders | Aug, 2022 will cowl the newest and most present steerage world wide. method in slowly so that you perceive capably and appropriately. will progress your data easily and reliably

Trendy Android Improvement (MAD) for multimedia functions

Meet our summer time Android DevRel Engineer intern, Avish Parmar!

Hey everybody!

I spent my summer time as a developer relations engineer intern on Google’s Android DevRel group. Upon becoming a member of Google, I used to be excited in regards to the alternative to make an influence within the developer neighborhood. On this weblog submit, I will talk about fashionable approaches to constructing Android media apps by my expertise changing the Common Android Media Participant (UAMP) media participant pattern app to Compose, updating it to make use of fashionable libraries like Media3.

As somebody utterly new to Android growth with a background in Java, studying Kotlin was pleasantly trivial. Equally, I used to be in a position to depend on my data of declarative frameworks like React to study Jetpack Compose, Android’s fashionable UI toolkit. Listed here are some similarities I seen:

  • Like React, Compose permits builders to construct person interfaces and concurrently see what they’re creating utilizing Compose’s preview function.
  • Compose’s syntax is just like React, however a lot cleaner since all markup, akin to strings for various languages, is hidden in an app’s useful resource recordsdata and is referenced by the suitable useful resource features.
Implementing buttons in React
Implementing buttons in Compose

My purpose was to remodel UAMP to include the rules of Trendy Android Improvement (MAD). To get began, I wanted to grasp the structure of Android media functions to familiarize myself with how UAMP is applied.

Right here is the final structure of UAMP:

music service:

UAMP was not too long ago migrated to make use of the brand new Jetpack Media3 library, which permits Android functions to create wealthy visible and audio experiences. Media3 consolidates legacy APIs akin to Jetpack Media (also called MediaCompat), Jetpack Media2, and Exoplayer2. It contains a less complicated structure that’s acquainted to right now’s builders and lets you extra simply construct and keep your functions.

MusicService represents the “server” side or the “again finish” of UAMP.

Here’s a diagram describing MusicService:

MusicService It’s made up of two parts, MediaSession and the Participant. A MediaSession represents a media playback session in progress. It supplies numerous mechanisms to manage playback, obtain standing updates, and retrieve metadata in regards to the present media. makes use of of UAMP ExoPlayerMedia3 default Participant implementation, for playback.

A MediaController is used to speak with the MediaSession. the MediaControllerwhich is applied by MusicServiceConnection in UAMP and can be applied by different shoppers, it’s used to speak with the MediaSession.

For extra info on Media3, see the Media3 documentation.

person interface:

UAMP makes use of the Mannequin-View-ViewModel Structure to permit abstraction and a division of duty between every layer.

My foremost job in changing UAMP to Compose was to rewrite the views as Compose screens, connecting them to their respective ViewModels.

UAMP has three foremost UI courses: an exercise and two fragments:

  • MainActivity is accountable for swapping between the 2 shards.
  • MediaItemFragment is in control of shopping the music catalog. Exhibits an inventory of media gadgets that may be albums or songs. Touching an album will show a brand new MediaItemFragment containing the songs inside that album. Enjoying a track will begin enjoying that track and show the NowPlayingFragment.
  • NowPlayingFragment reveals the track that’s presently enjoying.

The 2 fragments can be transformed to Compose whereas holding MainActivity roughly intact for conversion functions.

UAMP makes use of each view binding and information binding to work together with Views.

View binding lets you extra simply write code that interacts with views and isn’t wanted in Compose. Information binding lets you bind UI parts in your layouts to information sources in your software utilizing a declarative (relatively than programmatic) format.

Listed here are three approaches to changing an app that makes use of view binding to Compose:

  1. Take away the adapters (eg MediaItemAdapter in UAMP) used to configure binding and use these values ​​to populate Composables as an alternative. This method is environment friendly as a result of you do not have to alter the underlying framework configured by the respective Fragment and the code might be redirected to make use of Composables.
  2. In case your app makes use of Fragments, eradicating the Fragments and creating the screens from scratch is one other method that will work, though the method is usually a bit extra cumbersome because the Fragments implementation might comprise some logic that can should be re-implemented within the composable features.
  3. To keep up view bindings, one method could be to have just some components of the UI applied in Compose. Views would nonetheless be accessed utilizing their respective view features, akin to binding.view_id_name.setText(). Composables might be accessed by way of binding.composeview_id_name.apply /* All of the Compose code right here */ .

I selected to delete the MediaItemAdapter and preserve the snippets so you possibly can reuse enterprise logic already applied in UAMP. This was potential as a result of reactive frameworks like Compose take away the necessity to bind views.

For the info binding, I used the observeAsState operate to bind composable to LiveData. Each time there’s a change to the related LiveData, the Compose display is recomposed and the modifications are propagated.

After you set observeAsState for a media aspect’s place attribute, Compose takes over the state administration duties for you.

With that performed, I began changing UI parts to Compose. For instance, I modified the RecyclerView nonetheless LazyColumn.

Earlier than: RecyclerView definition in XML format
Earlier than: Enterprise logic to fill RecyclerView with media parts
After: LazyColumn in Compose handles each defining the UI and filling the listing with gadgets

The final step was to return a ComposeView within the fragments onCreateView features (for instance, see MediaListFragment‘s onCreateView operate right here) to show the corresponding display. Screens comprise the composable features that outline the person interface and the controllers that join the screens to the enterprise logic embedded within the view fashions.

The play/pause icon defines a controller that invokes the suitable view mannequin operate to play/pause the presently enjoying media.

I additionally created a Compose display, NowPlayingScreenwhich makes use of Composables that individually show related metadata in regards to the presently enjoying merchandise, akin to album artwork, title, and length.

Media3 makes it straightforward to undertake new options like spatial audio, which is a function that makes your app play sound as if it have been coming from digital audio system positioned across the listener for a extra lifelike expertise. With the current launch of Media3 1.0.0-beta02, ExoPlayer is up to date to configure the platform for multichannel spatial audio.

To include spatial audio, I created a configuration display, which might enable the person to toggle spatial audio within the app and retrieve info from the Spatializer API for the presently enjoying media. That is how the toggle management was applied within the app:

5 Code snippets to explain the method.

SettingsScreen Swap and toggleSpatialAudio implementation
MainActivityViewModel toggleSpatialization sends a customized command to MusicService by way of MusicServiceConnection
spatializationBehavior the audio attribute is about for the ExoPlayer occasion
onAudioAttributesChanged is known as on MusicServiceConnection which then posts the up to date worth to isAppSpatializationEnabled
The isAppSpatializationEnabled observer on the NowPlayingFragmentViewModel calls updateState, which updates the worth of spatializationStatus.

Compose was an effective way to get into Android growth as a result of I used to be in a position to depend on my expertise with different declarative frameworks and simplify the code I wrote. With the ability to interoperate Compose with XML made it straightforward to transform from UAMP fragments to Compose screens, as you possibly can leverage the underlying enterprise logic that was already used for Views. Compose additionally allowed me to “see what I coded” through the use of the @Preview annotation to visualise part and attribute modifications in a real-time UI preview. Since Compose took care of state administration like view bindings, I used to be in a position to focus solely on making the UAMP UI lovely and interactive.

New libraries are being actively launched to Compose to boost present performance and add new options. To become involved, you possibly can be a part of the Compose neighborhood and share your suggestions. With a thriving developer neighborhood and frequent updates, it is easy to search out options to any points chances are you’ll encounter. The neighborhood has already constructed high-quality libraries to deal with common use circumstances, akin to dynamic imaging. I extremely suggest any developer, new or previous, to a minimum of give Compose a strive when constructing a brand new app, including new screens to an present app, or migrating an app to Compose.

Coming again to Media3, the simplicity and compatibility it presents by way of being primarily a bridge between numerous media libraries is a testomony to its effectivity and effectiveness. The Media3 library makes it straightforward for builders to include fashionable options, akin to spatial audio, into their functions, whereas requiring much less code and having a a lot less complicated structure. From a developer’s viewpoint, Media3 is the one cease store for all of the media performance chances are you’ll want.

By engaged on this undertaking, there are various issues that I’ve realized; not solely technically but additionally psychologically as a developer. Early on, studying about Kotlin and the Jetpack libraries allowed me to broaden my data and ability set. Delving somewhat deeper into my position as a developer relations engineer intern, I noticed the worth of Roman Nurik’s “Stroll Then Discuss” philosophy at DevRel, which implies not solely pondering critically about finest practices, but additionally placing myself within the place of different builders, whose multitude of use circumstances these libraries purpose to fulfill. I hope I’ve satisfied you of the advantages of adopting fashionable Android growth methods to your media app.

Trendy libraries like Compose and Media3 are repeatedly launched and up to date, making it straightforward for builders to benefit from new instruments and options. Whereas MAD raises the bar, it additionally reduces the training curve for brand spanking new builders to get began with Android growth due to its simplicity.

For extra info on UAMP:


For extra info on Compose:


For extra info on Media3:


To discover ways to migrate an app to Compose:


I hope the article nearly MAD about Media. Trendy Android Improvement (MAD) for… | by Android Builders | Android Builders | Aug, 2022 provides perspicacity to you and is beneficial for addendum to your data

MAD about Media. Modern Android Development (MAD) for… | by Android Developers | Android Developers | Aug, 2022

By admin