2016-12-20 4 views
0

백그라운드 모드에서 미디어 재생에 문제가 있습니다. 서버에서 소켓 연결을 통해 알림을 보내면 백그라운드 모드에서 미디어 재생을 재생해야합니다. 제 경우에는 미디어 플레이어가 백그라운드 모드에서 앱을 사용할 때 잘 작동합니다. 음악 앱을 재생하고 음악 플레이어를 중지하고 내 앱에 알림을 보내면 앱이 백그라운드 모드에있을 때 문제가 발생합니다. 내 앱이 미디어 플레이어를 재생하지 않은 경우 문제가 발생합니다. plist에 "AirPlay를 사용하여 오디오 또는 스트림 오디오/비디오 스트리밍"을 추가했습니다. "AVPlayer"를 사용하고 있습니다.배경 음악 모드에서 내 앱 미디어 재생이 실행되지 않았습니다. 사과 음악 앱을 열면

도와주세요. 감사합니다. .

답변

0

AVPlayer 설명서를 보면 한 번에 하나의 애셋 만 재생할 수 있다고 나와 있습니다. 나는 Apple Music이 이와 관련없는 동일한 API가 아니라면 사용한다고 생각합니다.

AVPlayer는 한 번에 하나의 미디어 자산을 재생하기위한 것입니다. 플레이어 인스턴스는 의 replaceCurrentItem 사용하여 추가 미디어 자산을 재생 재사용 할 수 있습니다 (와 :) 방법을하지만, 한 번

0

1에서 단 하나의 미디어 자산의 재생을 관리합니다). 당신 AppDelegate.m (didFinishLaunchingWithOptions) 방법이 코드를 넣어 :

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; 
[[AVAudioSession sharedInstance] setActive: YES error: nil]; 
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; 

2). 사진 오디오, 에어 플레이와 사진에 대한 활성이 설정 배경 모드

enter image description here

+0

는 이미 AppDelegate에 이러한 코드를 추가하고 배경 모드도에있다. – Giresh

+0

내 측면에서 완벽하게 작동합니다. 하지만 저는 YMCAudioPlayer를 사용하여 음악을 연주했습니다. –

+0

다음 단계에 따라 테스트 해주세요. 1. 앱 재생 미디어를 엽니 다. 2. 플레이어를 중지합니다. 3. 사과 음악 앱을 열고 음악을 연주 한 다음 음악을 중지하십시오. 4. 이제 앱이 백그라운드 모드에 있습니다. 백그라운드 모드에서 앱 미디어 플레이어를 재생 해 봅니다. 결과보기 – Giresh

0

후 현재 AVPlayer를 실행 항목으로 중단 할 그 때문에, 될 수 있습니다.

또한 AVPlayer에 항목을로드하기 전에 코드 (코드 아래)를 구현했는지 확인하십시오. 백그라운드에서 오디오를 재생할 수있는 기능을 제공합니다.

인터럽트 처리기를 구현하고 추가하면 다른 오디오/인터럽트가 중지 된 후에 중지 된 오디오를 다시 재생할 수 있습니다. 그 아래 청취자가 전화 할 것입니다.

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

및 관리 AVPlayer를 플레이/그것을 다시 일시 :

- (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 
      IsInteruptionOccured=YES; 
     } break; 
     case AVAudioSessionInterruptionTypeEnded:{ 
      // • Make session active 
      // • Update user interface 
      // • AVAudioSessionInterruptionOptionShouldResume option 
      IsInteruptionOccured=NO; 
      [[AVAudioSession sharedInstance] setActive: YES error: nil]; 
      if (interruptionOption.unsignedIntegerValue == AVAudioSessionInterruptionOptionShouldResume) { 
       // Here you should continue playback. 
       // Resume after exteranl interruption. 


        [_audioPlayer play]; 


       BOOL isPlayingWithOthers = [[AVAudioSession sharedInstance] isOtherAudioPlaying]; 
       // test it with... 
       NSLog(@"other audio is playing %d",isPlayingWithOthers); 
      } 
     } break; 
     default: 
      break; 
    } 
}