AVplayer resuming after incoming call

前端 未结 6 1505
心在旅途
心在旅途 2020-12-04 15:48

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?

6条回答
  •  温柔的废话
    2020-12-04 16:34

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

提交回复
热议问题