iOS检测耳机插入拔出

首先,需要导入两个框架

然后,注册通知检测耳机的插入与拔出操作

1     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(outputDeviceChanged:)name:AVAudioSessionRouteChangeNotification object:[AVAudioSession sharedInstance]];
2     [[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(@"没有耳机");
    }
        
    }
    
            
}
原文地址:https://www.cnblogs.com/luerniu/p/4977893.html