Since requestAudioFocus(AudioManager.OnAudioFocusChangeListener l, int streamType, int durationHint)
is deprecated in API O , how do you set audio focus change listener using AudioFocusRequest
https://developer.android.com/guide/topics/media-apps/audio-focusOfficial Link for the resource
AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mAudioManager.requestAudioFocus(new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN)
.setAudioAttributes(
new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_GAME)
.setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
.build()
)
.setAcceptsDelayedFocusGain(true)
.setOnAudioFocusChangeListener(new AudioManager.OnAudioFocusChangeListener() {
@Override
public void onAudioFocusChange(int focusChange) {
//Handle Focus Change
}
}).build()
);
} else {
mAudioManager.requestAudioFocus(focusChange -> {
//Handle Focus Change for earliest version of oreo
},
AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With