Android: Detect another application has started pl

2019-01-13 08:25发布

My music application constantly plays music in the background, however I'd like to be able to detect when another application starts playing audio (such as the YouTube app) so I can pause/mute/stop the audio in my application.

This will allow a user to continue browsing the web whilst listening to music, but then if they wish to watch a video at any point, they can do so without audio conflict.

One solution might be to listen for a broadcast which states when an application begins using the AudioManager. Does such an Intent Action exist?


Edit: As in the answer provided below, there appears to be a method of detecting the loss of audio focus in 2.2 with AudioManager.OnAudioFocusChangeListener.

Great, but is there a solution for the more common versions of Android? Ideally 1.5+.