int result = audioManager.requestAudioFocus(focusChangeListener,
AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
startMediaPlayer(songPath);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
audioManager.registerAudioPlaybackCallback(new AudioManager.AudioPlaybackCallback() {
@Override
public void onPlaybackConfigChanged(List<AudioPlaybackConfiguration> configs) {
super.onPlaybackConfigChanged(configs);
boolean isFoundPlayOtherMusic = false;
if (configs.size() > 1) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
if (configs != null) {
for (int i = 0; i < configs.size(); i++) {
if (configs.get(i).getAudioAttributes().getUsage() == USAGE_MEDIA && (configs.get(i).getAudioAttributes().getContentType() == CONTENT_TYPE_MUSIC || configs.get(i).getAudioAttributes().getContentType() == CONTENT_TYPE_UNKNOWN)) {
isFoundPlayOtherMusic = true;
}
}
if (isFoundPlayOtherMusic) {
controls.pauseControl(getApplicationContext());
} else {
if (audioFocus == AudioManager.AUDIOFOCUS_LOSS ||
audioFocus == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT ||
audioFocus == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
controls.playControl(getApplicationContext());
}
}
}
} else {
controls.pauseControl(getApplicationContext());
}
} else if (configs.size() == 0) {
if (audioFocus == AudioManager.AUDIOFOCUS_LOSS ||
audioFocus == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT ||
audioFocus == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK)
controls.playControl(getApplicationContext());
}
}
}, handler);
}
here is the code that check the audio focus of audio manager, is audio manager gain to success focus then play the Music Player, I also implement the audioChangeFocusListener as below code.
private AudioManager.OnAudioFocusChangeListener focusChangeListener =
new AudioManager.OnAudioFocusChangeListener() {
public void onAudioFocusChange(int focusChange) {
switch (focusChange) {
case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK:
System.out.println("===== AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK : " + focusChange);
break;
case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
System.out.println("===== AUDIOFOCUS_GAIN_TRANSIENT : " + focusChange);
break;
case AudioManager.AUDIOFOCUS_GAIN:
System.out.println("===== AUDIOFOCUS_GAIN : " + focusChange);
if (!mp.isPlaying())
controls.playControl(getApplicationContext());
break;
case AudioManager.AUDIOFOCUS_LOSS:
System.out.println("===== AUDIOFOCUS_LOSS : " + focusChange);
if (mp.isPlaying())
controls.pauseControl(getApplicationContext());
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
System.out.println("===== AUDIOFOCUS_LOSS_TRANSIENT : " + focusChange);
if (mp.isPlaying())
controls.pauseControl(getApplicationContext());
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
System.out.println("===== AUDIOFOCUS_LOSS : " + focusChange);
}
}
};
Audio Focus is not working in android 9 and 10 both. I am using audio play with notification in my app.
Audio Focus not Gain and Loss in Our app, is it possible to stop our app music when audio play from other app, and when I play audio in my app then stop other music player from other app, is it possible then how?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…