2013-12-23 3 views
22

음악 재생에 AVPlayer을 사용하고 있습니다. 내 문제는 들어오는 호출 후에 플레이어가 다시 시작되지 않는다는 것입니다. 전화가 왔을 때 어떻게 처리합니까?전화 수신 후 AVplayer 재개

답변

7

AVAudioSession은 중단이 시작되고 끝날 때 알림을 보냅니다. 이 당신이 위임 방법을 사용했다 전에

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

우리가 AVAudioSession는보다 자세한 인터페이스를 제공하기 때문에 내가 CTCallCenter을 제안하지 않을이 – user2889249

+0

에 대한 CTCallCenter를 사용할 수 있습니다 만 재 장전 플레이어는 문제를 해결하기 위해 나에게 도움이됩니다. 사용되지 않는 메소드를 제거하기 위해 내 대답을 업데이트했습니다. –

54

Handling Audio Interruptions, 당신은 AVAudioSessionInterruptionNotificationAVAudioSessionMediaServicesWereResetNotification를 처리해야합니다 아이폰 OS 6 일부터 시작을 참조하십시오.

먼저 AVAudioSession 싱글 톤을 호출하고 원하는 용도로 구성해야합니다.

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

두 번째는 med ia 서버가 어떤 이유로 재설정되면 오디오를 재구성하거나 하우스 키핑을 수행하려면이 알림을 처리해야합니다. 그런데 알림 사전에는 어떤 객체도 포함되지 않습니다. 옵션 값이 AVAudioSessionInterruptionOptionShouldResume

입니다 그리고 다른 통지를 다음의주의해야 할 때 재생을 다시 시작해야

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

공지 사항 : 여기

는 재생 중단을 처리하기위한 예입니다 :

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

감사합니다.

+0

설명서에서 MediaServicesWereReset을 처리 할 때 "응용 프로그램에서 사용하는 모든 오디오 객체를 다시 초기화하는 적절한 단계를 수행합니다."라고 말하면 AudioSession을 다시 구성하고 다시 활성으로 설정합니다. 내가해야 할 일은 뭐니? – xialin

1

으로 전화를 걸어도 내 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() 
    } 
    }