My iOS game has music and sound effects. I'd like to let users listen to their own music in place of the game's background music.
A simple solution is to add a new menu item that disables the game's background music. However, I'd like to avoid creating a new menu item unless I can be convinced that this approach is worse for the user.
My current approach:
- Set the audio session category to
AVAudioSessionCategoryAmbient
to allow mixing of game audio with iPod (or other music app) playback. - In
applicationDidBecomeActive
, check[[AVAudioSession sharedInstance] isOtherAudioPlaying]
and turn off the game's music if another app is playing in the background.
This seems to work under most circumstances. When the game launches, it interrupts the backgrounded music app's playback. If you want to play your own music, you then have to deliberately go back and press play in the third-party music app, or use iOS 7's swipe-up control panel. Otherwise, the game's own music will take over.
This comes with a couple flaws:
- It does not detect when the third-party music app's playback is started/stopped by the click-button remote control that's built into iPhone/iPod earbuds.
- Although it works with the iPod app, it doesn't work with the SoundCloud app. I have not tested any others.
The question:
Is there a way to receive notifications when third-party music apps (e.g. the iPod app) start/stop playback? Alternatively, is there a way to be notified when the value of @property BOOL otherAudioPlaying
changes?
Can this approach backfire? Is it foolish not to simply use a menu item?