# Examples on handling iOS AVAudioSession notifications?

**URL:** <https://qa.fmod.com/t/examples-on-handling-ios-avaudiosession-notifications/14244>\
**Category:** FMOD Engine\
**Created:** [December 10, 2018, 11:42pm UTC](https://qa.fmod.com/t/examples-on-handling-ios-avaudiosession-notifications/14244 "2018-12-10T23:42:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![yangtgc](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/yangtgc/32/54_2.png) [@yangtgc](https://qa.fmod.com/u/yangtgc)\
**Post date:** [December 10, 2018, 11:42pm UTC](https://qa.fmod.com/t/examples-on-handling-ios-avaudiosession-notifications/14244/1 "2018-12-10T23:42:01Z")

</div>

I get really confused on how to handle AVAudioSession notifications to satisfy the expectation of FMOD Studio on iOS.

I had a look at `common_platform.mm` found in the examples provided with Fmod Studio API. I find it listens to many `AVAudioSession` notifications, but all of them are simply printing logs without doing anything real. Even for `AVAudioSessionInterruptionNotification` where it calls `gSuspendCallback`, I find `gSuspendCallback` is not set anywhere. Does that mean I’m NOT expected to do anything with these notifications?

In contrast, the [documentation](https://www.fmod.com/resources/documentation-api?page=content/generated/platform_ios/basics.html#/) explicitly says I should (or might? ) call `mixerSuspend` and `mixerResume` for `addObserverForName:AVAudioSessionInterruptionNotification`. I cannot find any call to these two functions in example code.

So should I call anything into FMOD for `AVAudioSession` notifications? Especially, how should I properly handle `AVAudioSessionInterruptionNotification`, `AVAudioSessionRouteChangeNotification` and `AVAudioSessionMediaServicesWereResetNotification`? Do I need to suspend or resume or reset anything of FMOD?

Is there example code that actually handles those notifications?

Thanks,

---

<div class="post-metadata">

**Author:** ![cameron-fmod](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/cameron-fmod/32/261_2.png) [@cameron-fmod](https://qa.fmod.com/u/cameron-fmod)\
**Post date:** [December 13, 2018, 2:48am UTC](https://qa.fmod.com/t/examples-on-handling-ios-avaudiosession-notifications/14244/2 "2018-12-13T02:48:40Z")

</div>

The callback code in the common files are more for our own internal testing.

You will _NEED_ to use mixerSuspend & mixerResume when handling interruptions, otherwise you will run into problems as FMOD will not be allowed to run in the background.

While these are not used in any of our examples, the code in the documentation you linked is the best place to start.

---

<div class="post-metadata">

**Author:** ![yangtgc](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/yangtgc/32/54_2.png) [@yangtgc](https://qa.fmod.com/u/yangtgc)\
**Post date:** [December 13, 2018, 6:09am UTC](https://qa.fmod.com/t/examples-on-handling-ios-avaudiosession-notifications/14244/3 "2018-12-13T06:09:34Z")

</div>

First of all, I think if it’s called “examples” all the code included in it should be “examples” that we’re supposed to learn from, considering this is a closed-source software.

Second, the documentation of mixerSuspend says “All internal state will be maintained”, which I think it’s not true. When mixer suspends, the timeline playhead does not move forward. As a result, if the gameplay code keep playing sound, all of them will be stuck in memory and never finishes, and eventually run out of memory.

We considered adding a check around the PlaySound() function provided to gameplay code like `if (mixerSuspended) then play else do nothing`. But I realized it is also wrong: If I add this check, and if a BGM music is supposed to play on level load, and if the AVAudioSession is interrupted during level load, then the music never plays even when the session restores.

You must be already aware that it’s not only going to background that leads to the interrupted state, but also a phone call can hold the interrupted state for a long time even when the app is in the foreground. And we want to allow players to play during a phone call without running out of memory by all queued but not played sound.

So the situation is more complicated than just call mixerSuspend() on interruption begin and mixerResume() on interruption end. So it’ll be super helpful to have some actual example code that is not for internal testing.

---

<div class="post-metadata">

**Author:** ![cameron-fmod](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/cameron-fmod/32/261_2.png) [@cameron-fmod](https://qa.fmod.com/u/cameron-fmod)\
**Post date:** [December 16, 2018, 11:29pm UTC](https://qa.fmod.com/t/examples-on-handling-ios-avaudiosession-notifications/14244/4 "2018-12-16T23:29:47Z")

</div>

Thank you for the feedback, we are currently in the process of going over our examples and documentation.

MixerSuspend maintains the internal state by keeping everything alive in memory, any calls made to FMOD during this time will be added to the queue and be processed once MixerResume is called.  
Because of this, MixerSuspend should be used when the game is in a paused state.

To play sounds in the background, you will need to configure the AudioSession to handle this:  
[https://developer.apple.com/library/archive/documentation/Audio/Conceptual/AudioSessionProgrammingGuide/AudioSessionBasics/AudioSessionBasics.html](https://developer.apple.com/library/archive/documentation/Audio/Conceptual/AudioSessionProgrammingGuide/AudioSessionBasics/AudioSessionBasics.html)
