iOS 监听耳机插入和拔出[检索]

来源:互联网 发布:java orm框架 阿里 编辑:程序博客网 时间:2024/05/03 12:54

iOS 监听耳机插入和拔出[检索]


使用通知的方式,添加收到通知的回调方法
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioRouteChangeListenerCallback:)   name:AVAudioSessionRouteChangeNotification object:nil];
响应通知回调。
- (void)audioRouteChangeListenerCallback:(NSNotification*)notification{NSDictionary *interuptionDict = notification.userInfo;NSInteger routeChangeReason = [[interuptionDict valueForKey:AVAudioSessionRouteChangeReasonKey] integerValue];switch (routeChangeReason) {case AVAudioSessionRouteChangeReasonNewDeviceAvailable:NSLog(@"AVAudioSessionRouteChangeReasonNewDeviceAvailable");NSLog(@"耳机插入");break;case AVAudioSessionRouteChangeReasonOldDeviceUnavailable:NSLog(@"AVAudioSessionRouteChangeReasonOldDeviceUnavailable");NSLog(@"耳机拔出,停止播放操作");break;case AVAudioSessionRouteChangeReasonCategoryChange:// called at start - also when other audio wants to playNSLog(@"AVAudioSessionRouteChangeReasonCategoryChange");break;}}
0 0