添加系统监听
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(yinpinInterruption:) name:AVAudioSessionInterruptionNotification object:nil];
实现回调代理
- (void)yinpinInterruption:(NSNotification *)notification{
NSDictionary *interuptionDict = notification.userInfo;
NSInteger interuptionType = [[interuptionDict valueForKey:AVAudioSessionInterruptionTypeKey] integerValue];
NSNumber* seccondReason = [[notification userInfo] objectForKey:AVAudioSessionInterruptionOptionKey] ;
switch (interuptionType) {
case AVAudioSessionInterruptionTypeBegan:
{
NSLog(@"收到中断,停止音频播放");
break;
}
case AVAudioSessionInterruptionTypeEnded:
NSLog(@"系统中断结束");
break;
}
switch ([seccondReason integerValue]) {
case AVAudioSessionInterruptionOptionShouldResume:
NSLog(@"恢复音频播放");
if (用条件判断是否可以音频播放) {
NSLog(@"恢复播放");
}
break;
default:
break;
}
}
注意:恢复音频播放时候,应当有条件,比如原先音频在播放状态下,才可以恢复播放。
网友评论