From iOS 6 onwards you can set your audio session active, play your file and then deactivate your session with options using the AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation flag. Make sure you set a non-mixable category when you need to play audio so background audio stops.
In simple steps -
// Configure audio session category and activate ready to output some audio
[[AVAudioSession sharedInstance] setActive:YES error:nil];
// Play some audio, then when completed deactivate the session and notify other sessions
[[AVAudioSession sharedInstance] setActive:NO withOptions: AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];
From Apple's documentation-
When passed in the flags parameter of the setActive:withOptions:error: instance method, indicates that when your audio session deactivates, other audio sessions that had been interrupted by your session can return to their active state.
This flag is used only when deactivating your audio session; that is, when you pass a value of NO in the beActive parameter of the setActive:withOptions:error: instance method
and also-
Deactivating your session will fail if any associated audio objects (such as queues, converters, players or recorders) are currently running.
EDIT: A more detailed example -
Configure a mixable audio session at the start of your applications lifecycle
// deactivate session
BOOL success = [[AVAudioSession sharedInstance] setActive:NO error: nil];
if (!success) { NSLog(@"deactivationError"); }
// set audio session category AVAudioSessionCategoryPlayAndRecord
success = [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];
if (!success) { NSLog(@"setCategoryError"); }
// set audio session mode to default
success = [[AVAudioSession sharedInstance] setMode:AVAudioSessionModeDefault error:nil];
if (!success) { NSLog(@"setModeError"); }
// activate audio session
success = [[AVAudioSession sharedInstance] setActive:YES error: nil];
if (!success) { NSLog(@"activationError"); }
When your application wants to output audio without any background audio playing, change the audio session category first like this
// activate a non-mixable session
// set audio session category AVAudioSessionCategoryPlayAndRecord
BOOL success;
AVAudioSessionCategoryOptions AVAudioSessionCategoryOptionsNone = 0;
success = [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionsNone error:nil];
if (!success) { NSLog(@"setCategoryError"); }
// set audio session mode default
success = [[AVAudioSession sharedInstance] setMode:AVAudioSessionModeDefault error:nil];
if (!success) { NSLog(@"setModeError"); }
// activate audio session
success = [[AVAudioSession sharedInstance] setActive:YES error: nil];
if (!success) { NSLog(@"activationError"); }
// commence playing audio here...
And when your application has completed playing audio you can deactivate your audio session
// deactivate session and notify other sessions
// check and make sure all playing of audio is stopped before deactivating session...
BOOL success = [[AVAudioSession sharedInstance] setActive:NO withOptions: AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error: nil];
if (!success) { NSLog(@"deactivationError"); }
I can confirm that the above code works, tested with the Music App playing on an iPhone 5 running iOS 7.0.4, however this is not guaranteed because there are other considerations such as user actions. For example if I plug in a headset the background audio from the music app routes to the headset and continues to play, but if I remove the headset the background audio produced by the music app pauses.
For more info read AVAudioSession Class Reference