当前app在播放音频,此时打开另外一个app,或者系统铃声响起,会我们的app被打断的现象,此时我们需要暂停我们的播放界面,以及其它一系列的动作,
那我们需要获取到当前打断的这个事件
系统提供了一个打断通知 供我们进行打断处理
AVAudioSessionInterruptionNotification
- 注册通知
[ [NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioSessionInterrupted:) name:AVAudioSessionInterruptionNotification object:nil];
- 处理通知
- (void)audioSessionInterrupted:(NSNotification *)notification
{
//通知类型
NSNumber *interruptionType = [[notification userInfo] objectForKey:AVAudioSessionInterruptionTypeKey];
AVAudioSessionInterruptionOptions options =
[info[AVAudioSessionInterruptionOptionKey] unsignedIntegerValue];
switch (interruptionType.unsignedIntegerValue) {
case AVAudioSessionInterruptionTypeBegan:{
[self postNotification:StopMusicByInterruptNotification];
} break;
case AVAudioSessionInterruptionTypeEnded:{
if (options == AVAudioSessionInterruptionOptionShouldResume) {
//触发重新播放
}
[self postNotification:InterruptEndNotification];
} break;
default:
break;
}
}