Android Media Playback

Musa Tondolo
Droid Wall
Published in
6 min readAug 13, 2018
Photo by rawpixel on Unsplash

Using media in Android apps is a common task and nearly every use case shares common features.These common features are:

  • Media player: This is a component that can take in digital media and render it as audio or video.
  • Media controller: This is the piece of the UI that controls the media player and includes all of the UI buttons such play, pause, rewind and forward.

There are many options when it comes to these components including building from the scratch for your specific needs , but the nature of their interaction is nearly the same for every application.

That is why the Android Framework provides two classes that impose a well defined structure for building media player apps. The two classes are media session and media controller. These two classes communicate with each other so that the UI stays in sync with the player.They communicate using predefined callbacks that correspond to standard player actions such as play, stop, pause, rewind and forward. They also allow you to include extensible custom calls that you can use to define special behaviors that are unique to your app.

Implementing Media Session and Media Player in Android

Implementing media session and media player in Android depends on the use case. Apps that play audio exclusively have different requirements to apps that play both audio and video. When playing both audio and video, the UI is expected to remain visible for the duration of the playback since the user is watching the video. So , navigating away from the UI should normally terminate the playback. However, when playing only video, you will most likely want the playback to continue even if the user navigates away from the UI.

Media Playback Life Cycle

For video apps, the life cycle of the player is completely tied to the life cycle of the UI. Therefore, all components of the media player can be implemented in a single activity which contains both the media controller the media session.

For audio apps, the player may out live the activity that started it if the user navigates away .Therefore, your media session should run in a service, which updates your UI activity when the player state changes.

Comparing Media Players

The Android framework provides a number of options on the types of media players to use in your app.

  • Media Player: Media player provides the basic functionality for a bare bones player that supports the most common audio and video formats and data sources. This player supports very little customization but is very straightforward to use and is good enough for many use cases.
  • ExoPlayer: This is an open source library that exposes the lower-level Android audio APIs. ExoPlayer supports high performance features like DASH and HLS (adaptive streaming technologies) which are not found Media Player. Further, you can customize code hence making it easy to add new components.
  • YouTube: If your use case is playing video specifically from YouTube, you can checkout the YouTube developer APIs.
  • Custom Media Player: You can also build a custom media player from low-level media APIs such as MediaCodec, AudoTrack and MediaDRM, that meets the exact needs of your media app.

Media Formats

Media exists in a variety of different formats at different levels. From highest level to the lowest level, these include:

  • Sample Formats: These are the formats of the individual media samples. Example include: H.264 for video and AAC for audio.
  • Container Formats: This houses the media samples and the associated metadata. Example include: MP4 for video and MP3 for audio.
  • Adaptive Streaming Technologies: These are not media formats as such, however, it is necessary to define the level of support ExoPlayer supports. Examples include DASH, smooth streaming and HLS.

Steps to Implement Media Playback (ExoPlayer) in Android

Before, you implement ExoPlayer in your Android app, ensure that the following requirements:

  • A recent version of Android Studio (>=3.0)
  • An Android device with Android 4.1 or greater.

Once you have confirmed, proceed to implement ExoPlayer by following these steps on Codelabs.

Customizing SimpleExoPlayerView

ExoPlayer comes with two notable out of the box UI elements:

  • PlaybackControlView: This is a view for controlling ExoPlayer instances. It displays standard playback controls including a play/pause button, fast-forward and rewind buttons, and a seek bar.
  • SimpleExoPlayerView: It is a high level view for SimpleExoPlayer media playbacks. It displays video (or album art in our case) and displays playback controls using a PlaybackControlView.

These ExoPlayer UI components can be customized in the following ways: follows:

  • Attributes: The XML items support a variety of XML attributes that customize the look of the UI. Take a look at the documentation for the UI element to see the list of possible attributes (and their corresponding Java methods).
  • Overriding Layout Files: When these views are inflated, they use specific layout files to determine what the UI looks like. For SimpleExoPlayerView, this file is called: exo_simple_player_view.xml. This layout file includes a PlayBackControlView (once it’s inflated, it replaces the exo_controller_placeholder item) which also uses its own layout file: exo_playback_control_view.xml. If you include any layout files with the same names, ExoPlayer will use them instead of these default ones. This allows you to fully customize what the UI looks like.

Other ExoPlayer Features

  • Subtitle Side Loading: Given a video file and a separate subtitle file, MergingMediaSource can be used to merge them into a single source for playback.
MediaSource videoSource = new ExtractorMediaSource(videoUri, ...);
MediaSource subtitleSource = new SingleSampleMediaSource(subtitleUri, ...);
// Plays the video with the sideloaded subtitle.
MergingMediaSource mergedSource =
new MergingMediaSource(videoSource, subtitleSource);
  • Looping a Video: A video can be seamlessly looped using a LoopingMediaSource. The following example loops a video indefinitely. It’s also possible to specify a finite loop count when creating a LoopingMediaSource.
MediaSource source = new ExtractorMediaSource(videoUri, ...);
// Loops the video indefinitely.
LoopingMediaSource loopingSource = new LoopingMediaSource(source);

Other Android Media Framework Features

  • Audio Focus: This is how the Android framework knows about different applications using audio. If you want your app to fade out when other important notifications (such as navigation) occur, you’ll need to learn how your app can “hop in line” to be the one in charge of audio playback, until another app requests focus.
  • Noisy Intent: There are certain conditions that you will want to check for. For example, imagine you are blasting your favorite song at full volume. Right when it’s about to get to the best part, you trip and yank out the headphones from the audio port. Suddenly the whole world knows your secret. Not the best experience right? Luckily the android framework sends out the ACTION_AUDIO_BECOMING_NOISY intent when this occurs. This allows you to register a broadcast receiver and take a specific action when this occurs (like pausing the music and saving yourself of embarrassment).
  • Audio Stream: Android uses separate audio streams for playing music, alarms, notifications, the incoming call ringer, system sounds, in-call volume, and DTMF tones. This allows users to control the volume of each stream independently. By default, pressing the volume control modifies the volume of the active audio stream. If your app isn’t currently playing anything, hitting the volume keys adjusts the ringer volume. To ensure that volume controls adjust the correct stream, you should call setVolumeControlStream() passing in AudioManager.STREAM_MUSIC.

Conclusion

You should choose the media player that makes the most sense for your use case. But generally the customizability of ExoPlayer makes it the preferred choice since its support of many different formats and extensible modular features makes it usable in almost every use case. Further, because ExoPlayer is a Library that you include in your APK, you have control over which version you use and you can easily update to a newer version as part of a regular application update.

--

--