After Google’s official announcements at Android Dev Summit 2021
Jetpack Media 3 has come to play. With the official rollout of Jetpack Media3(v1.0) on 23 March 2023

Jetpack Media 3 is a set of libraries that help work with media files on Android devices. Earlier, there were two separate libraries, Media 2 and ExoPlayer 2, which had similar and different capabilities, making it hard to decide which one to use for different situations.

But now, AndroidX Media is a library that provides a unified way to work with media files. This makes it easier for developers to handle media files on Android devices. Some of the new features in AndroidX Media include:

Before we get deep into media3 deep we need to know some of the key components of Media3 :

Media app components
  • Player: An interface that defines traditional high-level functionality for an audio or video player, such as playback controls.
  • ExoPlayer: The default implementation of the Player interface in Media3.
  • MediaSession: An API that advertises media playback to and receives playback command requests from external clients.
  • MediaSessionService: A service that holds a MediaSession to enable background playback.
  • MediaLibraryService: A service that additionally allows you to expose a content library to external clients.
  • MediaController: An API that is generally used by external clients to retrieve playback information and send playback command requests to your media app. Complementary to a MediaSession. Examples of external clients include the notification and lock screen media controls on mobile and large screen devices, Android Auto, WearOS, and Google Assistant.
  • MediaBrowser: An API that additionally enables external clients to navigate your media app’s content library. Complementary to a MediaLibraryService.

Originally published by Nevin Mital on the Android Developer Blog.

What’s new in Media 3?

A unified media API: AndroidX Media introduces a new unified media API that makes it easier for developers to work with media files. The new API provides a consistent way to access and manipulate media files across different types of media sources, such as local files, network streams, and content providers.

Media session management: AndroidX Media provides built-in support for media session management, which allows developers to create media sessions and control media playback using standard system controls such as the notification and lock screen.

  • Problem: With Media 2 & Exoplayer, there was a need to add a separate connector since the media controller & session were not directly compatible with Exoplayer
  • Solution: With Media3 all the components such as Media Session, and Media Controller are based on the same common player interface which makes them compatible with each other & removes the usage of the connector between Media player & Media Session/Controller
#AndroidDevSummit

Media Library service: AndroidX Media provides a media browser service that allows developers to browse and play media content from different sources, such as media providers and apps.

With Media 3 in Play, we can easily

  • Define your own Custom Player
  • Have better control over your Media Session & expose it to other controllers
  • Expose Media Content & Integration support with other apps

Basic Demo Implementation

The implementation of Jetpack Media3 is quite similar to legacy Exoplayer 2 i.e. what we used to work on earlier.

Libraries :

def media3_version = "1.0.1"
implementation "androidx.media3:media3-exoplayer:$media3_version"
implementation "androidx.media3:media3-ui:$media3_version"
implementation "androidx.media3:media3-exoplayer-hls:$media3_version"

The Basic implementation for Media3 is similar to What we had in ExoPlayer

Migration from ExoPlayer to Media3 Player

  1. A migration guide to walk you through the process step-by-step
  2. A migration script to convert your standalone ExoPlayer project packages to the corresponding new modules and packages under Media3

Media Session Demo Implementation

Libraries :

def media3_version = "1.0.1"
implementation "androidx.media3:media3-exoplayer:$media3_version"
implementation "androidx.media3:media3-ui:$media3_version"
implementation "androidx.media3:media3-exoplayer-hls:$media3_version"
implementation "androidx.media3:media3-session:$media3_version"
implementation "androidx.media3:media3-common:$media3_version"

Setup Media Playback Service

The Player interface plays a key role in the architecture of Media3.

Step 1 : Setup Media Controller

  • In the Player-Activity set the MediaController this controller acts as the bridge between Media Player & its Session
  • Post setting up the media controller we can use the same controller to play the media item
val sessionToken = SessionToken(this, ComponentName(this, MediaPlaybackService::class.java))
val controllerFuture = MediaController.Builder(this,sessionToken).buildAsync()
controllerFuture.addListener({
val controller = controllerFuture.get()
controller.setMediaItem(mediaItem)
controller.prepare()
controller.play()
}, MoreExecutors.directExecutor())

Step 2 : Setup Media Playback Service

  • Media Playback Service is a MediaLibraryService that allows us to maintain & play any media item in the background
// Extend MediaSessionService
class MediaPlaybackService : MediaSessionService() {
private var mediaSession: MediaSession? = null
// Create your Player and MediaSession in the onCreate lifecycle event
override fun onCreate() {
super.onCreate()
val player = ExoPlayer.Builder(this).build()
mediaSession = MediaSession.Builder(this, player).build()
}
// Return a MediaSession to link with the MediaController that is making
// this request.
override fun onGetSession(controllerInfo: MediaSession.ControllerInfo): MediaSession?
= mediaSession
}
  • Make sure to register this service in Manifest & add the required foreground service permission if not added already
<service
android:name=".MediaPlaybackService"
android:foregroundServiceType="mediaPlayback"
android:exported="true">
<intent-filter>
<action android:name="androidx.media3.session.MediaSessionService"/>
</intent-filter>
</service>

With the introduction of Jetpack Media 3 all the functionality that Exoplayer used to support are already part of media 3 library bundle

Demo Video

References

Some Additional thoughts?

With the introduction of Jetpack Media 3 there will be lot of more things coming to picture that combines & eases all the existing use case that involve usage of Media Players .

Thank you for reading, stay tuned for amazing articles!

Catch me up at: https://twitter.com/its_pra_tick

Do not forget to give claps !! Happy Coding 🙂

Source link