9

I'm working on an audio streaming app and I'm having troubles with interruptions. I have read that I should use AVAudioSessionInterruptionNotification, but my problem is it's getting called only when the interruption starts and not when it ends. Here's my code.

static dispatch_once_t pred;
static Player *sharedAudioPlayer = nil;
dispatch_once(&pred, ^
              {
                  sharedAudioPlayer = [[self alloc] init];

                  [[NSNotificationCenter defaultCenter] addObserver:sharedAudioPlayer selector:@selector(playerItemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
                  [[NSNotificationCenter defaultCenter] addObserver:sharedAudioPlayer
                                                           selector:@selector(audioSessionInterrupted:)
                                                               name:AVAudioSessionInterruptionNotification
                                                             object:audioSession];
              });

and then

- (void)audioSessionInterrupted:(NSNotification*)notification {
NSDictionary *interruptionDictionary = [notification userInfo];
NSNumber *interruptionType = (NSNumber *)[interruptionDictionary valueForKey:AVAudioSessionInterruptionTypeKey];
if ([interruptionType intValue] == AVAudioSessionInterruptionTypeBegan) {
    NSLog(@"Interruption started");
    if (self.delegate && [self.delegate respondsToSelector:@selector(playerWasPaused)]) {
        [self.delegate playerWasPaused];
    }
} else if ([interruptionType intValue] == AVAudioSessionInterruptionTypeEnded){
    NSLog(@"Interruption ended");
    [player play];
} else {
    NSLog(@"Something else happened");
}

When a call starts I get printed twice "Interruption started", but when it ends I don't get anything. I even tried to check if something else is sent to the notification but still nothing. Any ideas?

Shaik Riyaz
  • 11,204
  • 7
  • 53
  • 70
halfblood17
  • 657
  • 1
  • 6
  • 21
  • 1
    Make sure sharedAudioPlayer is on the main thread as theses notifications are sent on the main thread only. Also I noticed you have intValue where it should be integerValue as AVAudioSessionInterruptionTypeBegan and AVAudioSessionInterruptionTypeEnded are of type NSUInteger not int – Bamsworld Mar 12 '14 at 11:33
  • It may sound stupid but how do I put sharedAudioPlayer on the main thread? (By the way, I replaced intValue with integerValue but nothing changed). – halfblood17 Mar 12 '14 at 11:58
  • Did you happen to remove the observer AVAudioSessionInterruptionNotification Somewhere? – user523234 Mar 12 '14 at 12:03
  • @user523234 no, I don't. – halfblood17 Mar 12 '14 at 12:16
  • May I make one more suggestion, and that is where you register for the notification make the object argument nil instead of audioSession. I'm thinking the pointer to [AVAudioSession sharedInstance] will change after an interruption, and would explain why you get the began but not the ended notification. – Bamsworld Mar 12 '14 at 12:32
  • @Bamsworld just tried but still nothing :/ maybe the problem is really caused by the sharedAudioPlayer not running on the main thread. But how can I make it so that it runs on the main thread? Wouldn't it freeze the UI since it's a player? – halfblood17 Mar 12 '14 at 12:44
  • 2
    I don't want to mess with your design pattern but it may be worth considering registering say your app delegate or rootViewController to handle the notifications and leave the player to do its job. If say a controller object was to handle interruptions then it could send a message to the player, UI etc... – Bamsworld Mar 12 '14 at 12:48
  • @Bamsworld thank you! I've put the logic in the AppDelegate and now it works like a charm. Thanks again – halfblood17 Mar 12 '14 at 14:51
  • 2
    Is there any way to post the working code here as an answer? – Michael May 09 '14 at 18:58
  • similar question: http://stackoverflow.com/questions/31806155/ios-avaudiosession-interruption-notification-not-working-as-expected – NSDeveloper Aug 15 '15 at 10:24

0 Answers0