20

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+.

Kara
  • 6,115
  • 16
  • 50
  • 57
bdls
  • 4,578
  • 6
  • 24
  • 30

1 Answers1

10

http://developer.android.com/reference/android/media/AudioManager.OnAudioFocusChangeListener.html

this thread also has additional information that might get you heading in the right direction.

http://groups.google.com/group/android-developers/browse_thread/thread/db6822d84feaac6/219d8cba07795c61?hl=en&lnk=gst&q=OnAudioFocusChangeListener#219d8cba07795c61

Aaron Saunders
  • 33,180
  • 5
  • 60
  • 80
  • Thanks for your answer. This appears to be a solution though unfortunately for Froyo+ only. If it turns out there's no solution pre-Froyo, then it's great to see it now available, however I am ideally looking for a solution which will work 1.5+. – bdls Aug 28 '10 at 01:02
  • I've accepted your answer, thanks. It seems this is the only solution, so simply not possible in earlier versions of Android. – bdls Nov 23 '10 at 14:07