来电后AVplayer恢复

我正在使用AVPlayer进行音乐播放。 我的问题是,来电后,玩家将不会恢复。 来电时如何处理?

从iOS 6开始,您必须处理AVAudioSessionInterruptionNotificationAVAudioSessionMediaServicesWereResetNotification ,在此之前您必须使用委托方法。

首先,您应该调用AVAudioSession单例并将其configuration为您所需的用途。

例如:

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

那么你应该实现两个方法,AVAudioSession将调用的通知:

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

首先是由于来电,闹钟等原因而导致的任何中断。

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

第二个,如果媒体服务器由于任何原因重置,则应该处理该通知以重新configurationaudio或做任何pipe家。 顺便说一句,通知字典将不包含任何对象。

以下是处理播放中断的示例:

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

请注意,当可选值为AVAudioSessionInterruptionOptionShouldResume时,您应该继续播放

而对于另一个通知,你应该注意以下几点:

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

问候。

AVAudioSession将在中断开始和结束时发送通知。 请参阅处理audio中断

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

在某些情况下,即使我调用play()我的AVPlayer也不会继续播放。 只有重新加载播放器可以帮助我解决问题:

  func interruptionNotification(_ notification: Notification) { guard let type = notification.userInfo?[AVAudioSessionInterruptionTypeKey] as? UInt, let interruption = AVAudioSessionInterruptionType(rawValue: type) else { return } if interruption == .ended && playerWasPlayingBeforeInterruption { player.replaceCurrentItem(with: AVPlayerItem(url: radioStation.url)) play() } }