A little late to the party but for those looking for a solution to the problem of pausing and unpausing background (i.e. ipod music) after playing sounds, you should be using the following when deactivating your audio session.
[[AVAudioSession sharedInstance] setActive:NO withFlags:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:&error];
//New method that's not deprecated:
[[AVAudioSession sharedInstance] setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:&error];
Whenever you do not require the audio session in your application (i.e. when you're not outputting any sound) you should be deactivating it. Where it makes sense, you should use this method so that any background audio is notified and may resume (applications receiving this notification do not necessarily have to resume).
In addition, you must use the appropriate audio category to allow your sound to play exclusively where required. This can be done with any of the categories other than AVAudioSessionCategoryAmbient
. This will automatically pause the background audio for you when your Audio session becomes active. It will not, however, reactivate any background audio, that is done by using the above code. In which case, as stated earlier, background audio then decides what to do with the notification.
Side note, another option to look into is audio 'ducking'. If it's not imperative that your sound plays alone, such as a simple alert sound, try using ducking which will lower the volume of background audio to play your sound and when complete will raise the background audio back to normal when you're sound has finished playing. See Configure your Audio Session for more details on these concepts.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…