25

When I launch my iPhone game as soon as a sound plays the background music or podcast that is playing stops. I noticed other games allow background audio to continue to play.

How is this possible? Do I need to override a method in my App Delegate?

joe
  • 16,988
  • 36
  • 94
  • 131
  • possible duplicate of [iPhone AVAudioPlayer stopping background music](http://stackoverflow.com/questions/1672602/iphone-avaudioplayer-stopping-background-music) – t0mm13b Jan 03 '13 at 02:06

1 Answers1

59

Place this line in your application:didFinishLaunchingWithOptions: method of your AppDelegate or in general before using the audio player.

 [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];

According to the documentation, the AVAudioSessionCategoryAmbient category is

for an app in which sound playback is nonprimary—that is, your app can be used successfully with the sound turned off.

This category is also appropriate for “play along” style apps, such as a virtual piano that a user plays over iPod audio. When you use this category, audio from other apps mixes with your audio. Your audio is silenced by screen locking and by the Silent switch (called the Ring/Silent switch on iPhone).

If you want also to ensure that no error occurred you have to check the return value

NSError *error;
BOOL success = [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&error];
if (!success) {
     //Handle error
     NSLog(@"%@", [error localizedDescription]);
} else {
   // Yay! It worked!      
}

As a final remark, don't forget to link the AVFoundation framework to your project and import it.

#import <AVFoundation/AVFoundation.h>
Community
  • 1
  • 1
Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
  • 1
    Thanks, this did the trick. For other people's reference don't forget to import the AVFoundation. #import – joe Jan 02 '13 at 13:03
  • Is there any category which prevents the app from playing sounds if music is playing? – ZeMoon Jan 08 '14 at 12:15
  • 3
    I recommend putting it in applicationWillEnterForeground as well, else the user's audio will not resume if your app turns off the user's audio at some point and the user tries to resume their audio while re-opening your app, in which only an app restart would re-enable the setting. – Hellojeffy Mar 15 '15 at 17:41
  • Is there any way to prevent opening my app from stopping iTunes music if I'm using the playback audio session category? – vikzilla Feb 16 '16 at 05:56
  • 3
    @vikzilla yes, you can enable mixing with other audio session by using the `AVAudioSessionCategoryOptionMixWithOthers` option – Gabriele Petronella Feb 16 '16 at 12:42
  • It is worth noting that there are different categories for different needs: `ambient` for background sounds, `solo ambient`, `playback` for music tracks, `record`, `playAndRecord`, `audioProcessing` and `multiRoute`. It is worth checking which applies to you when setting your category – sam_smith Sep 21 '17 at 22:43