Pause/resume mediaPlayer on notification sound
It's easy t开发者_JS百科o 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.
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.
精彩评论