1.项目中, 有时候需要对耳机连接状态进行特殊处理, 这时候监听系统耳机连接通知是必不可少的
1.1 导入头文件
#import <AVFoundation/AVFoundation.h>
1.2 添加耳机连接变化通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didSessionRouteChange:) name:AVAudioSessionRouteChangeNotification object:[AVAudioSession sharedInstance]];
1.3 耳机连接变化
-(void) didSessionRouteChange:(NSNotification *)notification {
NSDictionary *interuptionDict = notification.userInfo;
NSLog(@"音频路由变化通知 判断输出设备 %@", interuptionDict);
AVAudioSessionRouteDescription *currentroute = [[AVAudioSession sharedInstance] currentRoute];
if (currentroute.outputs.firstObject.portType == AVAudioSessionPortHeadphones || currentroute.outputs.firstObject.portType == AVAudioSessionPortBluetoothA2DP || currentroute.outputs.firstObject.portType == AVAudioSessionPortUSBAudio) {
NSLog(@"连接耳机");
} else {
NSLog(@"当前无连接耳机");
}
}