Easy Audio Focus with ExoPlayer

Nicole Borrelli
AndroidX Media3
Published in
3 min readSep 27, 2018
Illustration by Virginia Poltrack

Managing audio focus is a vital part of being a good citizen in the Android media ecosystem.

What is audio focus? The docs describe it like this:

Two or more Android apps can play audio to the same output stream simultaneously. The system mixes everything together. While this is technically impressive, it can be very aggravating to a user. To avoid every music app playing at the same time, Android introduces the idea of audio focus. Only one app can hold audio focus at a time.

When your app needs to output audio, it should request audio focus. When it has focus, it can play sound. However, after you acquire audio focus you may not be able to keep it until you’re done playing. Another app can request focus, which preempts your hold on audio focus. If that happens your app should pause playing or lower its volume to let users hear the new audio source more easily.

Knowing when to play and when to pause playback is the key to ensuring a happy user versus one who feels like they’re caught in a shouting match.

In a continuing effort to live up to its name, we’ve enhanced SimpleExoPlayer in ExoPlayer 2.9 so that it manages audio focus automatically.

How to use it

Making use of this new feature is very simple. First, create an instance of com.google.android.exoplayer2.audio.AudioAttributes that matches your use case. For example if you’re playing a movie:

Then call setAudioAttributes with the second parameter set to true.

Now, SimpleExoPlayer will automatically manage audio focus for you. Your app should not include any code for requesting or responding to audio focus changes.

Pausing versus ducking

By default, when your app is playing audio and the device plays certain notifications, such as when a message arrives, or for turn by turn directions, your app’s playback volume will be reduced while the notification is playing.

That’s usually OK. But if the app is playing a podcast or an audiobook it would be better to pause than to continue playing softer, which could be distracting.

For these cases, you can setContentType to CONTENT_TYPE_SPEECH, and SimpleExoPlayer will pause while the notification is playing and will automatically resume afterwards.

Audio focus and player state

When you let ExoPlayer manage audio focus automatically, the behavior of two functions that return the state of playback needs a little explaining.

getPlayWhenReady

The value returned by getPlayWhenReady may surprise you in one case:

When playback temporarily pauses in response to a transient loss of audio focus, getPlayWhenReady() continues to return true. The reasoning is that the content will continue playing when audio focus is regained.

getVolume

When the volume is temporarily ducked due to a transient loss of audio focus, getVoume() returns the last value that you set (with setVolume()) — not the volume that you hear. This means setVolume and getVolume always agree. What you set is what you get… but not necessarily what’s heard.

Limitations

Audio focus can be permanent (AUDIOFOCUS_GAIN) or transient (AUDIOFOCUS_GAIN_TRANSIENT_*). For now, this automatic feature only works for usages that request permanent focus (USAGE_MEDIA or USAGE_GAME). If you setUsage to any other value, then setAudioAttributes will throw an IllegalArgumentException.

We are looking forward to seeing how the automatic handling of audio focus works in your app, and are eager to hear your feedback. File feedback and feature request on our GitHub Issue tracker. We’re keen to learn more about your requirements, and how we could make handling audio focus even easier!

--

--