AVAudioSessionDelegate called at endInterruption, but beginInterruption not called

7.5k Views Asked by At

I'm setting up an AVAudioSession when the app launches and setting the delegate to the appDelegate. Everything seems to be working (playback, etc) except that beginInterruption on the delegate is not being called when the phone receives a call. When the call ends endInterruption is being called though.

The only thought I have is that the audio player code I'm using used to be based on AVAudioPlayer, but is now using AVPlayer. The callbacks for the AVAudioPlayer delegate for handling interrupts are still in there, but it seems odd that they would conflict in any way.

5

There are 5 best solutions below

1
On

I just checked on my iPhone 5 (running iOS 6.0) by setting a breakpoint in the AudioSessionInterruptionListener callback function that was declared in AudioSessionInitialize(), and this interrupt callback does, in fact, get called when the app has an active audio session and audio unit and is interrupted with an incoming phone call (Xcode shows the app stopped at the breakpoint at the beginning of the interruption, which I then continue from).

I have the app then stop its audio unit and de-activate its audio session. Then, on the end interruption callback, the app re-activates the audio session and restarts the audio unit without problems (the app is recording audio properly afterwards).

1
On

Looking at the header, in iOS6, it looks like AVAudioSessionDelegate is now deprecated.

Use AVAudioSessionInterruptionNotification instead in iOS6.

Update: That didn't work. I think there's a bug in the framework.

Yes, in my experience, beginInterruption, nor the newly documented AVAudioSessionInterruptionNotification work properly. What I had to do was track the status of the player using a local flag, then handle the endInterruption:withFlags: method in order to track recovery from interruptions.

With iOS 6, the resuming from an interruption will at least keep your AudioPlayer in the right place, so there was no need for me to store the last known play time of my AVAudioPlayer, I simply had to hit play.

Here's the solution that I came up with. It seems like iOS 6 kills your audio with a Media Reset if an AVPlayer stays resident too long. What ends up happening, is the AVPlayer plays, but no sound comes out. The rate on the AVPlayer is 1, but there's absolutely no sound. To add pain to the situation, there's no error on either the AVAudioSession setActive, nor the AVPlayer itself that indicates that there's a problem.

Add to the fact that you can't depend on appWillResignActive, because your app may already be in the background if you're depending on remote control gestures at all.

The final solution I implemented was to add a periodic observer on the AVPlayer, and record the last known time. When I receive the event that I've been given back control, I create a new AVPlayer, load it with the AVPlayerItem, and seekToTime to the proper time.

It's quite an annoying workaround, but at least it works, and avoids the periodic crashes that were happening.

4
On

I can confirm that using the C api, the interruption method is also not called when the interruption begins; only when it ends

(AudioSessionInitialize (nil, nil, interruptionListenerCallback, (__bridge void *)(self));

I've also filed a bug report with apple for the issue.

Edit: This is fixed in iOS 6.1 (but not iOS 6.0.1)

1
On

Just call:

[[AVAudioSession sharedInstance] setDelegate: self];
0
On

I built a brand new audio streaming (AVPlayer) application atop iOS 6.0.x and found the same problem.

Delegates are now deprecated and we have to use notifications, that's great, however here's my findings:

  1. During an incoming phone call I get only AVAudioSessionInterruptionTypeEnded in my handler, along with AVAudioSessionInterruptionOptionShouldResume. Audio session gets suspended automatically (audio fades) and I just need to resume playback of AVPlayer.
  2. However when attempting to launch a game, such as CSR Racing, I oddly get the dreaded AVAudioSessionInterruptionTypeBegan but no sign when my application can resume playback, not even killing the game.

Now, this may depend on other factors, such as my audio category (in my case AVAudioSessionCategoryPlayback) and the mixing settings of both applications (kAudioSessionProperty_OverrideCategoryMixWithOthers), I'm not sure, but definitely I see something out of place.

Hopefully others reported that on 6.1beta this is fixed and I yet have to upgrade, so we'll see.