# 通知
/// 被打断通知
extern NSNotificationName const AVAudioSessionInterruptionNotification API_AVAILABLE(ios(6.0), watchos(2.0), tvos(9.0)) API_UNAVAILABLE(macos);
/// 路线改变通知(如,扬声器切换为耳机.也就是耳机插入设备)
extern NSNotificationName const AVAudioSessionRouteChangeNotification API_AVAILABLE(ios(6.0), watchos(2.0), tvos(9.0)) API_UNAVAILABLE(macos);
/// 当媒体服务丢失的时候的通知.
extern NSNotificationName const AVAudioSessionMediaServicesWereLostNotification API_AVAILABLE(ios(7.0), watchos(2.0), tvos(9.0)) API_UNAVAILABLE(macos);
/// 媒体服务重启时的通知
extern NSNotificationName const AVAudioSessionMediaServicesWereResetNotification API_AVAILABLE(ios(6.0), watchos(2.0), tvos(9.0)) API_UNAVAILABLE(macos);
/// 其他App占用/解除占用AVAudioSession通知
extern NSNotificationName const AVAudioSessionSilenceSecondaryAudioHintNotification API_AVAILABLE(ios(8.0), watchos(2.0), tvos(9.0)) API_UNAVAILABLE(macos);
# 通知中userInfo对应的key
/// `AVAudioSessionInterruptionNotification`的键,表示被中断的类型(AVAudioSessionInterruptionType)
extern NSString *const AVAudioSessionInterruptionTypeKey API_AVAILABLE(ios(6.0), watchos(2.0), tvos(9.0)) API_UNAVAILABLE(macos);
/// 仅用于中断事件中,类型为`AVAudioSessionInterruptionOptions `
extern NSString *const AVAudioSessionInterruptionOptionKey API_AVAILABLE(ios(6.0), watchos(2.0), tvos(9.0)) API_UNAVAILABLE(macos);
/// 仅仅在打断事件中,值为BOOL类型.ture表示是系统暂停的,false表示是其他的音频会话终端的.
extern NSString *const AVAudioSessionInterruptionWasSuspendedKey API_AVAILABLE(ios(10.3), watchos(2.3), tvos(10.3)) API_UNAVAILABLE(macos);
/// 用于`AVAudioSessionRouteChangeNotification`.值的类型是NSNumber.
extern NSString *const AVAudioSessionRouteChangeReasonKey API_AVAILABLE(ios(6.0), watchos(2.0), tvos(9.0)) API_UNAVAILABLE(macos);
/// 值是`AVAudioSessionRouteDescription`
extern NSString *const AVAudioSessionRouteChangePreviousRouteKey API_AVAILABLE(ios(6.0), watchos(2.0), tvos(9.0)) API_UNAVAILABLE(macos);
/// 用于`AVAudioSessionSilenceSecondaryAudioHintNotification`.值的类型是NSNumber.
extern NSString *const AVAudioSessionSilenceSecondaryAudioHintTypeKey API_AVAILABLE(ios(8.0), watchos(2.0), tvos(9.0)) API_UNAVAILABLE(macos);
AVAudioSessionInterruptionNotification (音频被中断通知)
/// MARK: 用到的枚举
typedef NS_ENUM(NSUInteger, AVAudioSessionInterruptionType)
{
AVAudioSessionInterruptionTypeBegan = 1, /* the system has interrupted your audio session */
AVAudioSessionInterruptionTypeEnded = 0, /* the interruption has ended */
};
/// MARK: 使用方式
/// 添加通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioSessionInterruptionNotification:) name:AVAudioSessionInterruptionNotification object:nil];
- (void)audioSessionInterruptionNotification:(NSNotification *)noti {
NSInteger InterruptionType = [[noti.userInfo objectForKey:AVAudioSessionInterruptionTypeKey] integerValue];
switch (InterruptionType) {
case AVAudioSessionInterruptionTypeBegan: {
NSLog(@"开始打断");
}
break;
case AVAudioSessionInterruptionTypeEnded: {
NSLog(@"结束打断");
}
break;
default:
break;
}
}
当我们的App正在播放音频,然后我们开启了其他App播放音乐或者是突然来电话了,那么我们就会收到一个
AVAudioSessionInterruptionNotification
通知,然后当我们应用重新进入前台.也就是active
状态的时候.我们会收到一个AVAudioSessionInterruptionTypeEnded
.告诉我们打断已经结束.可以重新让我们的播放器继续播放了.(一定要重新进入前台).
noti.userInfo @{@"AVAudioSessionRouteChangePreviousRouteKey" :<id AVAudioSessionRouteDescription> ,@"AVAudioSessionRouteChangeReasonKey" :@()}
/// 其中AVAudioSessionRouteChangePreviousRouteKey
AVAudioSessionRouteChangeNotification (播放线路改变通知)
当抽插耳机等造成播放线路改变时会发送的通知
userInfo信息:
- (void)audioSessionRouteChangeNotification:(NSNotification *)noti {
AVAudioSessionRouteDescription *routeDescription = [noti.userInfo valueForKey:AVAudioSessionRouteChangePreviousRouteKey];
NSInteger reason = [[noti.userInfo valueForKey:AVAudioSessionRouteChangeReasonKey] integerValue];
}
typedef NS_ENUM(NSUInteger, AVAudioSessionRouteChangeReason)
{
/// 未知原因
AVAudioSessionRouteChangeReasonUnknown = 0,
/// 有新设备可用
AVAudioSessionRouteChangeReasonNewDeviceAvailable = 1,
/// 老设备不可用
AVAudioSessionRouteChangeReasonOldDeviceUnavailable = 2,
/// 类别改变
AVAudioSessionRouteChangeReasonCategoryChange = 3,
/// App重置了输出设置
AVAudioSessionRouteChangeReasonOverride = 4,
/// 从睡眠状态唤醒
AVAudioSessionRouteChangeReasonWakeFromSleep = 6,
/// 当前category下没合适设备
AVAudioSessionRouteChangeReasonNoSuitableRouteForCategory = 7,
/// Rotuer的配置改变了
AVAudioSessionRouteChangeReasonRouteConfigurationChange = 8 // added in iOS 7
};
AVAudioSessionMediaServicesWereLostNotification (媒体服务丢失通知)
使用系统内置Developer功能的时候,会过一会儿才收到通知.此通知无
userInfo
.我们需要做的就是收到通知后看情况是否重启媒体服务.
AVAudioSessionMediaServicesWereResetNotification (媒体服务重启通知)
详情见上面的
AVAudioSessionMediaServicesWereLostNotification
详情.
AVAudioSessionSilenceSecondaryAudioHintNotification
其他设备占用了
AVAudioSession
通过userInfo
中的AVAudioSessionSilenceSecondaryAudioHintTypeKey
获取到的值有以下两种可能(类似上面的音频播放被中断)
AVAudioSessionSilenceSecondaryAudioHintTypeBegin 其他App开始占据Session
AVAudioSessionSilenceSecondaryAudioHintTypeEnd 其他App停止占用Session