注册一个通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioRouteChangeListenerCallback:) name:AVAudioSessionRouteChangeNotification object:[
AVAudioSession sharedInstance]];```
相应耳机发生变化的事件
/**
- 耳机通知
- @param notification 通知
*/
- (void)audioRouteChangeListenerCallback:(NSNotification*)notification{
NSDictionary *interuptionDict = notification.userInfo;
NSInteger routeChangeReason = [[interuptionDict valueForKey:AVAudioSessionRouteChangeReasonKey] integerValue];
switch (routeChangeReason) {
case AVAudioSessionRouteChangeReasonNewDeviceAvailable:
// NSLog(@"AVAudioSessionRouteChangeReasonNewDeviceAvailable");
//插入耳机
break;
case AVAudioSessionRouteChangeReasonOldDeviceUnavailable:
// NSLog(@"AVAudioSessionRouteChangeReasonOldDeviceUnavailable");
//拔出耳机
break;
case AVAudioSessionRouteChangeReasonCategoryChange:
// called at start - also when other audio wants to play
NSLog(@"AVAudioSessionRouteChangeReasonCategoryChange");
break;
}
}
欢迎关注我的[微博](http://weibo.com/u/2209572342/home?topnav=1&wvr=6&mod=logo)和[博客](http://dwz.cn/iOS_BK)。
欢迎打赏