3

So I have a music app that uses an AVAudioSession to allow it to play when it is in the background. I use this call:

[audioSession setActive:YES
            withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation
                  error:nil];

My problem now is if I go to another app and it steals the audio session (thus now stopping music playback from my app and playing something else), and I come back to my app, no matter what I do to reset my audio session or my audio units, my app's sound is gone.

Does anyone know what to do?

JackyJohnson
  • 3,106
  • 3
  • 28
  • 35

1 Answers1

5

So after registering for the AVAudioSession notifications:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(handleAudioSessionInterruption:)
                                             name:AVAudioSessionInterruptionNotification
                                           object:aSession]; 

You need to resume/restart you need to restart your player in the handler interruption type is AVAudioSessionInterruptionTypeEnded:

- (void)handleAudioSessionInterruption:(NSNotification*)notification {

    NSNumber *interruptionType = [[notification userInfo] objectForKey:AVAudioSessionInterruptionTypeKey];
    NSNumber *interruptionOption = [[notification userInfo] objectForKey:AVAudioSessionInterruptionOptionKey];

    switch (interruptionType.unsignedIntegerValue) {
        case AVAudioSessionInterruptionTypeBegan:{
            // • Audio has stopped, already inactive
            // • Change state of UI, etc., to reflect non-playing state
        } break;
        case AVAudioSessionInterruptionTypeEnded:{
            // • Make session active
            // • Update user interface
            // • AVAudioSessionInterruptionOptionShouldResume option
            if (interruptionOption.unsignedIntegerValue == AVAudioSessionInterruptionOptionShouldResume) {
                // Here you should continue playback.
                [player play];
            }
        } break;
        default:
            break;
    }
}

You can see a complete explanation here: AVplayer resuming after incoming call

Community
  • 1
  • 1
marosoaie
  • 2,352
  • 23
  • 32
  • the part with `AVAudioSessionInterruptionTypeBegan` lets me get the sound back when I return to my app, but the part with `AVAudioSessionInterruptionTypeEnded` never gets called. I suppose that depends on the interrupting app notification dispatch? Also, for the note, I'm not using AVAudioPlayer/AVPlayer – JackyJohnson Oct 07 '14 at 07:41
  • How are you testing this? What is triggering the interruption? – marosoaie Oct 09 '14 at 07:33
  • 1
    1. play music from my app. 2. leave app, go to Music app, play some music there (music from my app stops, delegate interruption began method gets called, music from Music app starts). 3. stop music in Music app (delegate interruption ended method doesn't get called) – JackyJohnson Oct 09 '14 at 08:42
  • Then I don't know what's wrong, sorry. It should work. I found this: http://stackoverflow.com/questions/7406389/iphone-ios-no-audio-interruption-ended-after-playing-video-in-safari . Maybe the bug they're talking about still isn't fixed, though I doubt it. – marosoaie Oct 10 '14 at 08:59