Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVplayer resuming after incoming call

I am using AVPlayer for music playback. My problem is that after an incoming call, the player won't resume. How do I handle this when an incoming call comes?

like image 884
user2889249 Avatar asked Dec 23 '13 04:12

user2889249


2 Answers

Starting from iOS 6 you must handle AVAudioSessionInterruptionNotification and AVAudioSessionMediaServicesWereResetNotification, before this you had to use delegate methods.

First you should call the AVAudioSession singleton and configure it for your desired use.

For example:

AVAudioSession *aSession = [AVAudioSession sharedInstance]; [aSession setCategory:AVAudioSessionCategoryPlayback           withOptions:AVAudioSessionCategoryOptionAllowBluetooth                  error:&error]; [aSession setMode:AVAudioSessionModeDefault error:&error]; [aSession setActive: YES error: &error]; 

Then you should implement two methods, for the notifications which the AVAudioSession would call:

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

First one is for any interruption which would be called because of an incoming call, alarm clock, etc.

[[NSNotificationCenter defaultCenter] addObserver:self                                          selector:@selector(handleMediaServicesReset)                                              name:AVAudioSessionMediaServicesWereResetNotification                                            object:aSession]; 

The second one if the media server resets for any reason, you should handle this notification to reconfigure audio or do any housekeeping. By the way the notification dictionary won't contain any object.

Here is an example for handling the playback interruption:

- (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;     } } 

Notice that you should resume playback when the optional value is AVAudioSessionInterruptionOptionShouldResume

And for the other notification you should take care of the following:

- (void)handleMediaServicesReset { // • No userInfo dictionary for this notification // • Audio streaming objects are invalidated (zombies) // • Handle this notification by fully reconfiguring audio } 

Regards.

like image 181
javiergov Avatar answered Sep 18 '22 19:09

javiergov


AVAudioSession will send a notification when an interruption starts and ends. See Handling Audio Interruptions

- (id)init {     if (self = [super init]) {         [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];          NSNotificationCenter *center = [NSNotificationCenter defaultCenter];         [center addObserver:self selector:@selector(audioSessionInterrupted:) name:AVAudioSessionInterruptionNotification object:nil];     } }  - (void)audioSessionInterrupted:(NSNotification *)notification {     int interruptionType = [notification.userInfo[AVAudioSessionInterruptionTypeKey] intValue];     if (interruptionType == AVAudioSessionInterruptionTypeBegan) {         if (_state == GBPlayerStateBuffering || _state == GBPlayerStatePlaying) {             NSLog(@"Pausing for audio session interruption");             pausedForAudioSessionInterruption = YES;             [self pause];         }     } else if (interruptionType == AVAudioSessionInterruptionTypeEnded) {         if ([notification.userInfo[AVAudioSessionInterruptionOptionKey] intValue] == AVAudioSessionInterruptionOptionShouldResume) {             if (pausedForAudioSessionInterruption) {                 NSLog(@"Resuming after audio session interruption");                 [self play];             }         }         pausedForAudioSessionInterruption = NO;     } } 
like image 34
Neal Ehardt Avatar answered Sep 19 '22 19:09

Neal Ehardt