iOS 如何监听出插入耳机 拔掉耳机事件?
来源:互联网 发布:剑灵最美灵女捏脸数据 编辑:程序博客网 时间:2024/06/10 09:39
#import<AVFoundation/AVFoundation.h>
//监听耳机事件
[[AVAudioSessionsharedInstance] setDelegate:self];
// Use this code instead to allow the app sound to continue to play when the screen is locked.
[[AVAudioSessionsharedInstance]setCategory:AVAudioSessionCategoryPlayback error:nil];
// Registers the audio route change listener callback function
AudioSessionAddPropertyListener(kAudioSessionProperty_AudioRouteChange,audioRouteChangeListenerCallback,self);
我个人推荐放到
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOption
因为是全局嘛 哪里都可以响应到
例如
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]]autorelease];
//登陆VC
LoginViewController *loginVC = [[LoginViewControlleralloc]init];
UINavigationController *navNV = [[UINavigationControlleralloc]initWithRootViewController:loginVC ];
[navNVsetNavigationBarHidden:YES];
[loginVC release];
//监听耳机事件
[[AVAudioSessionsharedInstance] setDelegate:self];
// Use this code instead to allow the app sound to continue to play when the screen is locked.
[[AVAudioSessionsharedInstance]setCategory:AVAudioSessionCategoryPlayback error:nil];
// Registers the audio route change listener callback function
AudioSessionAddPropertyListener(kAudioSessionProperty_AudioRouteChange,audioRouteChangeListenerCallback,self);
self.window.rootViewController = navNV;
[navNV release];
self.window.backgroundColor = [UIColorwhiteColor];
[self.windowmakeKeyAndVisible];
return YES;
}
//触发的监听事件
void audioRouteChangeListenerCallback (void *inUserData, AudioSessionPropertyID inPropertyID, UInt32 inPropertyValueSize,const void *inPropertyValue ) {
// ensure that this callback was invoked for a route change
if (inPropertyID != kAudioSessionProperty_AudioRouteChange)return;
{
// Determines the reason for the route change, to ensure that it is not
// because of a category change.
CFDictionaryRef routeChangeDictionary = (CFDictionaryRef)inPropertyValue;
CFNumberRef routeChangeReasonRef = (CFNumberRef)CFDictionaryGetValue (routeChangeDictionary, CFSTR (kAudioSession_AudioRouteChangeKey_Reason) );
SInt32 routeChangeReason;
CFNumberGetValue (routeChangeReasonRef, kCFNumberSInt32Type, &routeChangeReason);
if (routeChangeReason == kAudioSessionRouteChangeReason_OldDeviceUnavailable) {
//Handle Headset Unplugged
DLog(@"没有耳机!");
} else if (routeChangeReason == kAudioSessionRouteChangeReason_NewDeviceAvailable) {
//Handle Headset plugged in
DLog(@"有耳机!");
}
}
}
OK 搞定!
- iOS 如何监听出插入耳机 拔掉耳机事件?
- iOS 如何监听出插入耳机 拔掉耳机事件?
- iOS 如何监听出插入耳机 拔掉耳机事件?
- iOS 如何监听出插入耳机 拔掉耳机事件?
- iOS 如何监听出插入耳机 拔掉耳机事件?
- iOS 如何监听出插入耳机 拔掉耳机事件?
- iOS 如何监听出插入耳机 拔掉耳机事件?
- ios耳机拔掉侦听事件
- iOS 监听耳机插入和拔出[检索]
- 插入拔掉耳机流程 audio headset
- ios音乐播放耳机监听事件
- 监听耳机事件
- 监听耳机插孔事件
- Android监听耳机事件
- iOS 监听耳机状态
- iOS 监听耳机变化
- ios耳机监听
- iOS耳机侦听事件
- SurfaceView应用浅析
- SQL Server 无法生成 FRunCM 线程。请查看 SQL Server 错误日志和 Windows 事件日志(转)
- 关于使用myeclipse进行export 打包的一些注意点
- php中计算中文字符串长度、截取中文字符串的函数代码
- STL之set使用方法小结
- iOS 如何监听出插入耳机 拔掉耳机事件?
- 电话、地址、Email等常用正则表达式
- HDU 1074 (状态压缩DP)
- C/C++拾遗录--for循环中的三语句执行顺序
- vs2010安装报无法打开数据文件的问题
- 支持并发和延迟加载的单例模式
- RelativeLayout layout_alignBaseline:View中Content内容对齐
- java post 访问URL
- Base64算法的前世今生(三)