Handling Audio Focus In Android

Audio Focus is a very important part of the Media Player. Audio Focus handles playing of your audio source in the case of interruption. For example, if you are listening to a podcast and you interact  with a part of your app that plays a sound, that can be a song or anything else that comes to your mind, you’ll need to request Audio Focus. In some cases you will be releasing Audio Focus to other apps. That part comes to action when your app is playing some sort of an audio file and the other app wants to do the same thing.

When you are requesting Audio Focus you must specify a duration. That duration says to the other app how long are you going to play the audio file. You can request full Audio Focus. When you do that you are saying to the other app that you are going to be playing audio for a while. This approach is mostly used when writing a music player, or a podcast player. Most of the time when you start playing audio from a music player app you intent to stay in there for a while. There is no point in requesting from another app that it lowers its playing volume or that it pauses the audio playback if you’re going to be playing a song for the next five minutes (or twenty-five if you like Dream Theater).  When requesting permanent Audio Focus you must specify the AUDIOFOCUS_GAIN variable when calling the requestAudioFocus() method:

As you can see, the requestAudioFocus() method takes in three parameters. Those are:

  1. AudioManager.OnAudioFocusChangeListener: The listener to be notified of audio focus changes
  2. The stream type: This a int values. AudioManager specifies a couple of constant (we’re going to take a look at some of them in a minute)
  3. The duration hint: Also a  int constant specified by the AudioManager, in this example we’ve set it to AUDIOFOCUS_GAIN (we’re going to take a look at the rest of the constants in a moment)

Aside from AUDIOFOCUS_GAIN, there are a couple of other constants. The most noteworthy are:

  • AUDIOFOCUS_GAIN_TRANSIENT: You use it when you are going to be playing something for a short time. You expect the other app to pause its playback
  • AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK: Similar to the previous example, you use it when you’re going to be playing something for a short time, but the other app doesn’t pause its playback, it lowers its playback (“ducks”)

After finishing the playback, you should abandon the Audio Focus:

Responding to Audio Focus change

So, we’ve gone through requesting Audio Focus. Now it’ time to take a look at handling Audio Focus change. Similarly to  the example before, there are three ways you can lose Audio Focus:

  1. AUDIOFOCUS_LOSS: Permanent loss of Audio Focus. When your app receives this Audio Focus change, you should stop the playback completely.
  2. AUDIOFOCUS_LOSS_TRANSIENT: Temporary loss of Audio Focus. Usually the best to do when this change happens is to pause the playback:
  3. AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: Temporary loss of Audio Focus. Depending on your app’s needs you could pause the playback or just lower the volume (“duck”).
  4. AUDIOFOCUS_GAIN: Does this one seem familiar? That’s because we’ve used it before when we were requesting Audio Focus. When you receive this  Audio Focus change, you should resume the playback or set the volume to its normal value.