2016-06-02 7 views
1

AVAudioPlayer를 사용하는 다른 앱을 사용하는 동안 백그라운드에서 오디오를 재생하는 앱을 만들고 있습니다. 카메라를 열면 음악이 음소거되지만 AppDelegate for 앱 수명주기의 메소드가 호출되지 않으므로 노래의 재생 목록 위치 또는 재생 시간을 저장할 수 없습니다.카메라가 사라진 후에 오디오 재생을 다시 시작하는 방법은 무엇입니까?

또한 카메라를 닫을 때 앱에서 배경 음악 재생을 다시 시작하지만 다시이 변경 사항을 관찰 할 수있는 콜백 메소드를 찾지 못했습니다.

앱이 백그라운드 모드에서 실행 중일 때 카메라가 활성화되었고 카메라가 삭제 된 것을 보는 방법을 알고 있습니까?

+0

카메라는 어떻게 열 립니 까? – rmaddy

+0

슬라이드 업 제어판을 사용하면 어떻게 해오 고 있습니다. – Aaronium112

+0

이로 인해 앱이 백그라운드로 이동합니다. 난 그냥 그것을 테스트하고 적절한 애플 리케이션 대리자 메서드가 호출됩니다. – rmaddy

답변

1

어떻게 해결 했나요?

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

간섭을 처리하십시오.

-(void)handleAudioSessionInterruption:(NSNotification*)notification 
{ 
    //NSLog(@"%@",notification); 

    NSNumber *interruptionType = [[notification userInfo] objectForKey:AVAudioSessionInterruptionTypeKey]; //1 Interuption Start, 0 Interuption Ends 
    NSNumber *interruptionOption = [[notification userInfo] objectForKey:AVAudioSessionInterruptionOptionKey]; 

    if ([interruptionType integerValue] == AVAudioSessionInterruptionTypeBegan) 
    { 
     NSLog(@"Player %d interupted",playerNumber); 
     // • Audio has stopped, already inactive 
     // • Change state of UI, etc., to reflect non-playing state 
     [self.playPauseButton setTitle:@">" forState:UIControlStateNormal]; 
     self.playingAudio = NO; 

     return; 
    } 

    if ([interruptionType integerValue] == AVAudioSessionInterruptionTypeEnded) 
    { 
     if ([interruptionOption integerValue] == AVAudioSessionInterruptionOptionShouldResume) 
     { 
      NSLog(@"Player %d Resume",playerNumber); 
      [self.playPauseButton setTitle:@"||" forState:UIControlStateNormal]; 
      self.playingAudio = YES; 

      NSError *error = nil; 
      AVAudioSession *aSession = [AVAudioSession sharedInstance]; 
      [aSession setMode:AVAudioSessionModeDefault error:&error]; //& means value at address 
      [aSession setCategory:AVAudioSessionCategoryPlayback error:&error]; 
      //[aSession setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:&error]; 
      //[aSession setMode:AVAudioSessionModeSpokenAudio error:&error]; 
      [aSession setActive: YES error: &error]; 
      [self.audioPlayer play]; 
     } 
     // • Make session active 
     // • Update user interface 
     // • AVAudioSessionInterruptionOptionShouldResume option 
    }