Introducing Media3: The Next Generation of Android Media Framework

krupa parekh
Simform Engineering
6 min readMay 26, 2023

Whether you’re building a music player, a video streaming app, or any other Android application that needs to play media content, having a reliable media playback library is essential.

That’s where Media3 comes into the picture.

What is Media3?

Media3 is a powerful media playback library introduced by Google as a part of AndroidX. It provides an easy-to-use API that simplifies the implementation of media playback in Android applications.

With Media3, you can dive into designing immersive audio and video experiences.

If you’ve worked with ExoPlayer, MediaCompat, or Media2 before, you’ll feel right at home with Media3’s user-friendly interface.

Instead of deciding which library to use for a given use case, Media3 provides a single set of libraries that work well together and expands to cover new use cases like video editing and transcoding.

Basic Architecture of Media App

At its core, the architecture comprises two essential components: the Player and the User Interface (UI).

  • Player: The Player is responsible for rendering the audio and video content within the app. It handles complex tasks of decoding, buffering, and synchronizing media streams, ensuring a smooth playback experience for users.
  • UI: The user interface shows the user what content is playing and provides a way to control playback. It also displays metadata of content like title, duration, etc. The UI communicates with the player by sending commands like play, pause, seek, etc., and receiving callbacks when the state changes.

However, the Android operating system and other apps are unaware that your app is playing media. This can cause problems, so you need a media session.

Why choose MediaSession?

By establishing a connection to the MediaSession, you can expose information about the content being played to the system and enable playback control outside the app.

This makes a lot of helpful integrations possible. For instance,

  • On Android 11 and above, you can see and control playback from the media control box in the system UI.
  • If you have a wear OS device, you can control your app’s playback from your wrist using media notifications.
  • Using voice commands, you can ask other apps and Google Assistant to control playback for you.
  • It will automatically handle key media events like using a Bluetooth headset with play/pause buttons or a TV remote through HDMI CEC.

Creating MediaSessions can provide a better user experience by offering an easy way to view and control playback from various contexts.

Why is Media3 Better than Tradition APIs?

Let’s highlight the benefits of Media3 over traditional APIs:

Foreground playback

If your media app is designed to play content solely in the foreground, such as a basic video player, you can opt for the normal architecture of a media app. Your player, UI, and MediaSession are all contained within single Activity.

In the previous API, the MediaSession cannot communicate directly with the Player. You must have a connector object that translates commands and callbacks between the Player and the MediaSession. This could lead to bugs.

Media3 solves this problem by using ExoPlayer as its media player. ExoPlayer implements the player interface that both the UI widgets and MediaSession can accept. This means you can link them directly together without any connectors. This makes your app more streamlined and stable.

Background playback

Another feature is the background playback. This is when your media app plays content that can continue playing in the background, such as music, songs, or podcasts.

In this case, the architecture of our media app becomes slightly more complex. You need to separate the Service and the Activity. The Service contains the Player and creates a MediaSession to advertise playback. The Activity contains the UI and creates a MediaController to communicate with the MediaSession.

As we saw already, the Player can’t talk directly with MediaSession and thus requires a connector. Moreover, the MediaController has a different interface than the UI anticipates, requiring another connector. This can introduce complexity and increase the risk of errors.

Media3 eliminates the need for connectors. It does this by using one common Player interface throughout the framework. ExoPlayer implements this interface and is directly compatible with MediaSessions. It is also accepted by the UI widgets and implemented by the MediaController.

This means you can directly link the Player with the MediaSession and the MediaController with the UI. You don’t need any extra code to translate between them.

This is the primary characteristic that distinguishes Media3 from previous media APIs. This facilitates much simpler and more reliable interaction between the components.

What about ExoPlayer

According to the official announcement made by Google,

ExoPlayer is deprecated, long live ExoPlayer!

That’s right — As per the official blog, Google has announced that the ExoPlayer standalone library will soon be discontinued.

ExoPlayer has a new home and is the default implementation of the aforementioned Player interface in Media3. The standalone ExoPlayer project, with package name com.google.android.exoplayer2, will soon be discontinued, and future updates will be published in Media3.

This signifies that ExoPlayer 2.18.5 and ExoPlayer in Media3 1.0.0 are identical except for their package names. Nevertheless, it’s important to note that this arrangement is temporary, as the standalone ExoPlayer will be deprecated later this year. As a result, initiating the migration to Media3 at your earliest convenience is highly recommended.

Migrating to Media3

There are two key resources available to help you achieve this migration as smoothly as possible:

  1. An official 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.

The great news is that if you’re already using ExoPlayer, you won’t require any code modifications, nor will you need to reintegrate or rewrite any customizations.

As mentioned above, the standalone ExoPlayer and Media3 ExoPlayer are identical besides the package name. This means that migrating the package name can be easily automated by running a provided script. You just have to ensure you are using the latest version of Exoplayer.

Media3: New Features and Benefits over ExoPlayer

Media3 is a new media framework offering several benefits and features over its predecessor, ExoPlayer. Some of the key benefits of Media3 include:

  • Improved performance: Media3 is more efficient and uses less battery power, which can lead to better performance and longer battery life.
  • Expanded codec support: Media3 supports a wider range of codecs, meaning it can play various media files.
  • Simplified API: Media3 has a more straightforward API that is easier to use.
  • Improved security: Media3 includes several security features that help protect users' privacy.
  • Enhanced accessibility: Media3 includes several accessibility features that make it easier for people with disabilities to use.

In addition to these benefits, Media3 also includes several new features that were not available in ExoPlayer. Some of these include:

  • Support for adaptive streaming: Media3 supports adaptive streaming, allowing users to stream media files at the highest quality their device can support.
  • Support for multi-channel audio: Media3 supports multi-channel audio, which allows users to listen to audio in surround sound.
  • Support for hardware acceleration: Media3 supports hardware acceleration, which can improve performance on devices with powerful graphics processors.
  • Support for DRM: Media3 supports DRM, which allows users to play DRM-protected media files.

Summing up,

Media3 is a major upgrade from ExoPlayer and offers several benefits and new features that make it a better option for developers building media applications.

Stay tuned for more insightful content from Simform Engineering. Share your feedback and suggestions on what you would like to know next.

Follow us on Twitter and LinkedIn to stay updated on the latest insights, trends, and developments in the engineering world.

Happy coding :)

--

--