ios 耳机插入拔出检测

Posted Hai_阔天空

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ios 耳机插入拔出检测相关的知识,希望对你有一定的参考价值。

[AVAudiosession sharedInstance];
[[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(@"Headphone/Line plugged in");
break;

case AVAudioSessionRouteChangeReasonOldDeviceUnavailable:
NSLog(@"AVAudioSessionRouteChangeReasonOldDeviceUnavailable");
NSLog(@"Headphone/Line was pulled. Stopping player....");
break;

case AVAudioSessionRouteChangeReasonCategoryChange:
// called at start - also when other audio wants to play
NSLog(@"AVAudioSessionRouteChangeReasonCategoryChange");
break;
}
}



























以上是关于ios 耳机插入拔出检测的主要内容,如果未能解决你的问题,请参考以下文章

当我的应用程序处于后台模式时,检测从 iPhone 插入或拔出耳机插孔

插入/拔出耳机时如何获得通知?苹果

插入或拔出耳机时 iOS 应用程序崩溃

android耳机插拔监听做不了全局注册吗

插入/拔出耳机时 SKAudioNode() 崩溃

通过AppWidgetProvider判断耳机是不是插入,拔出时暂停音乐