Pause / resume MediaPlayer when notification sound

It's easy to detect when phone calls arrive (via phoneStateListener), but what about other notifications like email or sms?

On some devices, these notification sounds are muted, but do not pause any current media player instances that annoy the user. Ideally, I would like to listen to notifications that play sound, pause playback for a duration, and then resume playback afterwards.

+3
source share
1 answer

You can get a notification when another application wants to play audio by registering the AudioManager.OnAudioFocusChangeListener callback (this will also handle the case of an incoming call). In particular, you can search AUDIOFOCUS_GAIN, AUDIOFOCUS_LOSSand AUDIOFOCUS_TRANSIENT_LOSS. The Android music player source has a good example of this.

+3
source

Source: https://habr.com/ru/post/1781261/


All Articles