views:

623

answers:

1

I noticed that some apps programmatically mute itunes (if its running) at launching. How is this achieved? I have a game with background music and would like to either stop itunes or get at least a message that itunes is playing so that I can stop the game's background music.

thx, marc.

+5  A: 

You don't need to. With Audio Session you can decide how the audio should behave.

From the Audio Session Programming Guide:

With the audio session interface, you specify aspects of your application’s audio behavior and configure it to live harmoniously within the iPhone audio environment. You start by asking yourself questions such as these:

  • Do you want your audio to be silenced by the Ring/Silent switch? The answer is probably “yes” if audio is not essential to using your application successfully. (Users will appreciate being able to run your game in a meeting with no one the wiser.)

  • Do you want iPod audio to continue playing when your audio starts? This could be appropriate for a virtual piano, letting users play along to songs in their libraries. You’d want iPod audio to stop, however, for a streaming radio application.

You probably want this:

UInt32 sessionCategory = kAudioSessionCategory_SoloAmbientSound;
AudioSessionSetProperty (
    kAudioSessionProperty_AudioCategory,
    sizeof (sessionCategory),
    &sessionCategory
);

For more behaviour types, check the Audio Session Categories, or read the entire Audio Session Programming Guide.

Kriem