Request Audio Focus in Android via Service
Hello everyone, in this article we are going to talk about audio focus in android. When we build a music player program, sometimes during our program playing a music, we may want to open another program and maybe this related program will play another sound. At that time these both sounds will be played simultanously. No one want this. Because it is disturbing situtaion. So we need to pause our music or decrease the sound level at our program. We are going to control this in this article.Let's begin to write some code...
Here we are going to use AudioManager in android. AudioManager has OnAudioFocusChangeListener. The application listen the audio focus and when the other application started, our program will lose the AudioFocus.
First import the required library:
import android.media.AudioManager;
import android.app.Service;
Now we are going to create a class with constructor and extend it with Service. In here we will define AudioManager component variables. You can see what I mean below:
public class MusicPlayerService extends Service {
AudioManager mAudioManager;
private boolean isAudioFocusGranted = false;
final private AudioManager.OnAudioFocusChangeListener mOnAudioFocusChangeListener;
public MusicPlayerService()
{
}
}
Now I can register a listener when the service started. I prefer to make it at constructor method. After registering listener we will check it with a function.
Below code block you can see how to register it in constructor:
public class MusicPlayerService extends Service {
AudioManager mAudioManager;
private boolean isAudioFocusGranted = false;
final private AudioManager.OnAudioFocusChangeListener mOnAudioFocusChangeListener;
public MusicPlayerService()
{
mOnAudioFocusChangeListener = new AudioManager.OnAudioFocusChangeListener() {
@Override
public void onAudioFocusChange(int focusChange) {
switch (focusChange) {
case AudioManager.AUDIOFOCUS_GAIN:
Log.i("AUDIO_FOCUS", "AUDIOFOCUS_GAIN");
//Start the player when audio focus gain back
startPlayer();
break;
case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
Log.i("AUDIO_FOCUS", "AUDIOFOCUS_GAIN_TRANSIENT");
break;
case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK:
Log.i("AUDIO_FOCUS", "AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK");
break;
case AudioManager.AUDIOFOCUS_LOSS:
Log.e("AUDIO_FOCUS", "AUDIOFOCUS_LOSS");
//Pause the player when audio focus gain back
pausePlayer();
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
Log.e("AUDIO_FOCUS", "AUDIOFOCUS_LOSS_TRANSIENT");
//Pause the player when audio focus gain back
pausePlayer();
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
Log.e("AUDIO_FOCUS", "AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK");
break;
case AudioManager.AUDIOFOCUS_REQUEST_FAILED:
Log.e("AUDIO_FOCUS", "AUDIOFOCUS_REQUEST_FAILED");
break;
default:
//do whatever you want
}
}
};
}
}
Now we have registered our listener, we will check it when we want to check. Below code block will do it.
private boolean requestAudioFocus() {
//Check if audiofocus not granted
if (!isAudioFocusGranted) {
AudioManager am = (AudioManager) getAppContext().getSystemService(Context.AUDIO_SERVICE);
int result = am.requestAudioFocus(mOnAudioFocusChangeListener,
// Use the music stream.
AudioManager.STREAM_MUSIC,
// Request permanent focus.
AudioManager.AUDIOFOCUS_GAIN);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
isAudioFocusGranted = true;
} else {
// Audio Focus Gain failed
}
}
return isAudioFocusGranted;
}
public class MusicPlayerService extends Service {
AudioManager mAudioManager;
private boolean isAudioFocusGranted = false;
final private AudioManager.OnAudioFocusChangeListener mOnAudioFocusChangeListener;
private boolean requestAudioFocus() {
//Check if audiofocus not granted
if (!isAudioFocusGranted) {
AudioManager am = (AudioManager) getAppContext().getSystemService(Context.AUDIO_SERVICE);
int result = am.requestAudioFocus(mOnAudioFocusChangeListener,
// Use the music stream.
AudioManager.STREAM_MUSIC,
// Request permanent focus.
AudioManager.AUDIOFOCUS_GAIN);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
isAudioFocusGranted = true;
} else {
// Audio Focus Gain failed
}
}
return isAudioFocusGranted;
}
public MusicPlayerService()
{
mOnAudioFocusChangeListener = new AudioManager.OnAudioFocusChangeListener() {
@Override
public void onAudioFocusChange(int focusChange) {
switch (focusChange) {
case AudioManager.AUDIOFOCUS_GAIN:
Log.i("AUDIO_FOCUS", "AUDIOFOCUS_GAIN");
//Start the player when audio focus gain back
startPlayer();
break;
case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
Log.i("AUDIO_FOCUS", "AUDIOFOCUS_GAIN_TRANSIENT");
break;
case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK:
Log.i("AUDIO_FOCUS", "AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK");
break;
case AudioManager.AUDIOFOCUS_LOSS:
Log.e("AUDIO_FOCUS", "AUDIOFOCUS_LOSS");
//Pause the player when audio focus gain back
pausePlayer();
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
Log.e("AUDIO_FOCUS", "AUDIOFOCUS_LOSS_TRANSIENT");
//Pause the player when audio focus gain back
pausePlayer();
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
Log.e("AUDIO_FOCUS", "AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK");
break;
case AudioManager.AUDIOFOCUS_REQUEST_FAILED:
Log.e("AUDIO_FOCUS", "AUDIOFOCUS_REQUEST_FAILED");
break;
default:
//do whatever you want
}
}
};
}
private void fnc_playMusic(){
//....... MUSIC CONFIGURATIONS
if (!isAudioFocusGranted ) {
if(requestAudioFocus()){
// 2. Kill off any other play back sources
fnc_stopMusic();
}
}
//....... A LITTLE MORE MUSIC CONFIGURATIONS
}
}
We are ready for checking the AudioFocus. We can use it in our own music player applications.
That is all in this article
Have a good Requesting Audio Focus.
Burak hamdi TUFAN
Comments