音频中断无法正常工作

时间:2015-07-23 04:54:35

标签: objective-c audio avaudioplayer

我遇到音频中断正常工作的问题。在调试模式下,一切都在设备或模拟器上运行良好。但是,当设备与Xcode断开连接时,音频不会在来电后继续播放。请告诉我为什么会这样?

这是我的代码,位于AppDelegate

-(void)audioInterruption:(NSNotification*)notification
 {
   NSNumber *interruptionType = [[notification userInfo] objectForKey:AVAudioSessionInterruptionTypeKey];
   NSNumber *interruptionOption = [[notification userInfo] objectForKey:AVAudioSessionInterruptionOptionKey];
   switch (interruptionType.unsignedIntegerValue) {
       case AVAudioSessionInterruptionTypeBegan:{

                dispatch_async(dispatch_get_main_queue(), ^{
                     BOOL isPlayingWithOthers = [[AVAudioSession sharedInstance] isOtherAudioPlaying];

                     if(isPlayingWithOthers || [self.audioPlayer rate]>0)
                     {
                         [self.audioPlayer pause];
                         self.audioInterrupted = YES;
                      }
                   });

                break; 
            }
     case AVAudioSessionInterruptionTypeEnded:{

              if (interruptionOption.unsignedIntegerValue == AVAudioSessionInterruptionOptionShouldResume) {

              dispatch_async(dispatch_get_main_queue(), ^{

              if(self.audioInterrupted)
               {
                 [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
                 [[AVAudioSession sharedInstance] setActive: YES error: nil];
                 [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

                  [self.audioPlayer play];
                  self.audioInterrupted = NO;

               }
            });
        }

        break;
    }
    default:
        break;
  }
}

0 个答案:

没有答案