iOS音频播放处理来电话中断

在音频播放的基类里,init方法中或者viewdidload里添加

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioSessionInterruptionNotification:) name:AVAudioSessionInterruptionNotification object:nil];

----------------------------------------------------------------------

方法实现:

- (void)audioSessionInterruptionNotification:(NSNotification *)notification{

/*

系统方法监听到的中断事件通知,AVAudioSessionInterruptionOptionKey

typedef NS_ENUM(NSUInteger, AVAudioSessionInterruptionType)

{

AVAudioSessionInterruptionTypeBegan = 1, 中断开始

AVAudioSessionInterruptionTypeEnded = 0,  中断结束

}

*/


int type = [notification.userInfo[AVAudioSessionInterruptionOptionKey] intValue];

switch (type) {

case AVAudioSessionInterruptionTypeBegan: // 被打断

[self.player pause]; // 暂停播放

break;

case AVAudioSessionInterruptionTypeEnded: // 中断结束

[self.player play];  // 继续播放

break;

default:

break;

}

若按照系统的方法判断中断或者中断结束,会有时不响应,可以用此方法来判断

    if ([self.player isPlaying])

  {

       [self.player pause];

    }

    else

    {

       [self.player play];

    }

在dealloc中移除 NSNotificationCenter

你可能感兴趣的:(iOS音频播放处理来电话中断)