12

So in my app, running on iOS 6, everything seems to work fine with audio. I use the old C API format to catch interruptions using a callback; setting up via: AudioSessionInitialize(NULL, NULL, interruptionListenerCallback, (__bridge void *)self) and it was great. Using iOS 7 SDK though, it seems that my interruption callback is never called when the device receives calls or an alarm goes off.

After some looking around, I heard that the old C api were deprecated and that you should move to the newer AVAudioSession functions. More reading revealed that the AVAudioSession delegate is deprecated and that you should use the NSNotification for AVAudioSessionInterruptionNotification to catch interruptions and do whatever needs to be done.

For me, it seems that this notification is never actually fired and therefore I never get properly interrupted, which then breaks all of my audio stuff after the call ends.

I sign up for the notification like so:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(AudioInterruption:) name:AVAudioSessionInterruptionNotification object:nil];

For now, the AudioInterruption: function simply logs that it got fired. Neither the log nor any breakpoints are triggering.

To be clear, initially audio playback and recording works fine. When an interruption occurs (such as an incoming call or an alarm), no interruption notification is fired. If more surrounding code is necessary, let me know.

Doc
  • 1,480
  • 2
  • 16
  • 28
  • Just as a point, if I manually post the `AVAudioSessionInterruptionNotification` then my observer triggers, so that works. Still have no idea why the system isn't posting the notification itself. – Doc Nov 11 '13 at 15:58
  • Did you set the AVAudioSession mode and called `setActive` ? – imihaly Nov 12 '13 at 15:34
  • Yes, otherwise audio wouldn't play initially, which it does. `[session setMode:AVAudioSessionModeVoiceChat error:&error]` and `[[AVAudioSession sharedInstance] setActive:YES error:&activationError]`. I set the mode before adding the observer, and set active afterward, though it shouldn't matter (?). – Doc Nov 12 '13 at 15:44
  • What have you set for your audio categories? – H.Rabiee Nov 18 '13 at 13:23
  • I use `AVAudioSessionCategoryPlayAndRecord`. Experimenting with options yielded no changes regardless of choice. – Doc Nov 18 '13 at 14:22

3 Answers3

9

Do you have an AVCaptureSession instance in your application?
If that is the case, I would suggest the same answer that I received for my linked question:
Try to set NO to the usesApplicationAudioSession property of your AVCaptureSession instance.
It is a property available since iOS 7. In the previous iOS versions, each AVCaptureSession made use of a private AVAudioSession. Since iOS 7, the Capture Sessions make use of the shared application's AVAudioSession.
The usesApplicationAudioSession property is enabled by default, so if you want to keep the old behavior you must disable it, by setting NO.

I hope this will work for you as well.

Axel83
  • 197
  • 2
  • 13
  • I'll try it on Monday (or if I get the opportunity over the weekend, I'll run into the office to try it sooner). Crossing my fingers~ – Doc Jan 18 '14 at 08:19
  • 1
    This fixed my issue! Thanks so much for the help (and remembering to throw the answer over here after getting your own)! – Doc Jan 20 '14 at 21:57
  • 4
    what if [I don't have](http://stackoverflow.com/q/24205304/1387438) `AVCaptureSession`? Will this work if I create dummy object of `AVCaptureSession` (without inputs/outputs added)? – Marek R Jun 14 '14 at 10:17
2

You didn’t pass the AVAudioSession instance in your addObserver call as the last parameter.

Try this:

AVAudioSession *session = [AVAudioSession sharedInstance];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(AudioInterruption:)
                                             name:AVAudioSessionInterruptionNotification
                                           object:session];
Tes
  • 121
  • 1
  • 4
  • 7
    Passing the session isn't necessary, it just prevents the observer from picking up notifications from other objects. Just in case, I edited my code to pass in the session instead of nil, didn't help. Thanks for the suggestion though. – Doc Dec 27 '13 at 05:06
  • Actually this was the exact reason why I did not receive any notification on iOS 14. Also Apple's documentation states that you should pass a shared instance: https://developer.apple.com/documentation/avfaudio/avaudiosession/responding_to_audio_session_interruptions – Michael Oct 05 '21 at 22:21
-1

For me, activating audio session and adding observer when user actually push play button has resolved the issue; notification was fired. I think apple doc say something about that here.

LdiCO
  • 577
  • 12
  • 31