In the Audio Session Cookbook there is a section on "Checking if Other Audio is Playing During App Launch".
I'm using MonoTouch (but feel free to give an Objective-C answer, the API is the same), so the code from that document looks something like this:
bool otherAudioPlaying = AudioSession.OtherAudioIsPlaying;
if(otherAudioPlaying)
AudioSession.Category = AudioSessionCategory.AmbientSound;
else
AudioSession.Category = AudioSessionCategory.SoloAmbientSound;
The upshot is that, if my app is started with iPod music playing, that iPod music will keep playing and prevent me from using the hardware audio decoder (which I don't need anyway). If there's no iPod music playing when my app starts, I can use the hardware decoder.
I can then use the value of otherAudioPlaying
to determine whether or not to start playing my own music.
Now that works just fine on the older operating systems that don't support multitasking. But with multitasking the user can leave my app (backgrounding it), start music playing on the iPod, and re-enter my app. Then their iPod music will be silenced and my music will start playing again.
What I would like to have happen is have the iPod music continue playing when my application returns to the foreground - even if my own music was playing (and using the hardware decoder) when my application was backgrounded.
(And, in reverse - if my application was backgrounded with no music playing, I'd like to detect if I can start playing music when it returns).
How can I do this? (And preferably without abruptly cutting off my audio, maintaining the nice fade-out that usually happens.)
EDIT: I should also point out that I'm doing all my audio work off the main thread.