快速入门iOS

应用打断事件处理

2016-06-22  本文已影响4144人  Theshy

文档信息

/* Registered listeners will be notified when the system has interrupted the audio session and when
 the interruption has ended.  Check the notification's userInfo dictionary for the interruption type -- either begin or end.
 In the case of an end interruption notification, check the userInfo dictionary for AVAudioSessionInterruptionOptions that
 indicate whether audio playback should resume.
 */

    // 注册打断通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(AVAudioSessionInterruptionNotification:) name:AVAudioSessionInterruptionNotification object:session];
// 接收通知方法
  - (void)AVAudioSessionInterruptionNotification: (NSNotification *)notificaiton {
      NSLog(@"%@", notificaiton.userInfo);
  }

打电话 测试 打印出 AVAudioSessionInterruptionTypeKey = 1

/* keys for AVAudioSessionInterruptionNotification */
    /* value is an NSNumber representing an AVAudioSessionInterruptionType */

typedef NS_ENUM(NSUInteger, AVAudioSessionInterruptionType)
{
    AVAudioSessionInterruptionTypeBegan = 1,  /* the system has interrupted your audio session */
    AVAudioSessionInterruptionTypeEnded = 0,  /* the interruption has ended */
} NS_AVAILABLE_IOS(6_0);
// 接收通知方法
- (void)AVAudioSessionInterruptionNotification: (NSNotification *)notificaiton {
    NSLog(@"%@", notificaiton.userInfo);
    
    AVAudioSessionInterruptionType type = [notificaiton.userInfo[AVAudioSessionInterruptionTypeKey] intValue];
    if (type == AVAudioSessionInterruptionTypeBegan) {
        [self.player pause];
    } else {
        [self.player play];
    }
}

这样 当有电话打进来 就会停止播放,挂断电话继续播放

上一篇下一篇

猜你喜欢

热点阅读