首先,需要导入两个框架
然后,注册通知检测耳机的插入与拔出操作
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(outputDeviceChanged:)name:AVAudioSessionRouteChangeNotification object:[AVAudioSession sharedInstance]];
[[NSNotificationCenter defaultCenter]postNotificationName:AVAudioSessionRouteChangeNotification object:self];
通知方法
- (void)outputDeviceChanged:(NSNotification *)aNotification {
AVAudioSessionRouteDescription* route = [[AVAudioSession sharedInstance] currentRoute];
for (AVAudioSessionPortDescription* desc in [route outputs]) { if ([[desc portType] isEqualToString:AVAudioSessionPortHeadphones])
NSLog(@"有耳机");
else{
NSLog(@"没有耳机");
} } }