Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pause/resume mediaPlayer on notification sound

Tags:

android

It's easy to detect when phone calls come in (via phoneStateListener), but what about other notification sounds, such as email or sms?

On some devices, these notification sounds mute but don't pause any currently running mediaPlayer instances, which is annoying for the user. Ideally, I'd like to listen for notifications that play sound, pause playback for the duration, and then resume playback afterwards.

like image 704
GDorn Avatar asked Jan 02 '26 08:01

GDorn


1 Answers

You can get notified when another app wants to play audio by registering a callback on AudioManager.OnAudioFocusChangeListener (this would also handle the case of an incoming call). Specifically, you can look for AUDIOFOCUS_GAIN, AUDIOFOCUS_LOSS, and AUDIOFOCUS_TRANSIENT_LOSS. The Android music player source has a good example of this.

like image 130
Erich Douglass Avatar answered Jan 04 '26 05:01

Erich Douglass



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!