11

My app is playing in background well. Pause and play also works well even my app is background using the methods registered for AVAudioSessionInterruptionNotification.

The problem arrives steps for the scenario:

  1. Launch my app -> goes background music from my app playing well
  2. launch apple's music app and play. AVAudioSessionInterruptionNotification fired.
  3. When i pause the music app or kill the music app. AVAudioSessionInterruptionNotification doesn't get fired

Code: I am doing this in appdelegate. didfinishlaunching

NSError *sessionError = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&sessionError];
[[AVAudioSession sharedInstance] setActive:YES error: nil];

self.player = [[AVQueuePlayer alloc] initWithURL:[NSURL URLWithString:@"http://someurl.com:8040/;stream.mp3"]];

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




- (void) onAudioSessionEvent: (NSNotification *) notification
{
    //Check the type of notification, especially if you are sending multiple AVAudioSession events here
    NSLog(@"Interruption notification name %@", notification.name);

    if ([notification.name isEqualToString:AVAudioSessionInterruptionNotification]) {
        NSLog(@"Interruption notification received %@!", notification);

        //Check to see if it was a Begin interruption
        if ([[notification.userInfo valueForKey:AVAudioSessionInterruptionTypeKey] isEqualToNumber:[NSNumber numberWithInt:AVAudioSessionInterruptionTypeBegan]]) {
            NSLog(@"Interruption began!");

        } else if([[notification.userInfo valueForKey:AVAudioSessionInterruptionTypeKey] isEqualToNumber:[NSNumber numberWithInt:AVAudioSessionInterruptionTypeEnded]]){
            NSLog(@"Interruption ended!");
            //Resume your audio
            NSLog(@"Player status %i", self.player.status);
            // Resume playing the audio.
            [self.player play];

        }
    }
}
jailani
  • 2,260
  • 2
  • 21
  • 45
  • Were you able to figure it out? I'm having the same issue. – user1732055 May 21 '14 at 00:53
  • Having the same issue still. PlayAndRecord and Ambient session categories receive no "end of interruption" notification. SoloAmbient receives no notification at all. – kakyo Mar 12 '15 at 20:38
  • Did you do call -removeObserver: somewhere? I had a similar problem and that was the cause. If you're like me, you might want to do a grep/find on "removeObserver" just in case your eyes don't see it... – prewett Jul 07 '15 at 21:13

3 Answers3

0

I had similar problem with interrupts. iOS: Siri not available does not return AVAudioSessionInterruptionOptionShouldResume

Ended up using applicationWillResignActive and applicationDidBecomeActive instead.

Community
  • 1
  • 1
0

There is Property of your AVCaptureSession instance: @property(nonatomic) BOOL usesApplicationAudioSession

It is YES by default just set it to NO, and will works fine.

Singh_Nindi
  • 190
  • 2
  • 14
0

Try to add object:session

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAudioSessionEvent:) name:AVAudioSessionInterruptionNotification object:[AVAudioSession sharedInstance]];

Saravanan
  • 7,637
  • 5
  • 41
  • 72
Roc.Liu
  • 1
  • 1