iOS 如何监听出插入耳机 拔掉耳机事件?
来源:互联网 发布:淘宝客手机模板 编辑:程序博客网 时间:2024/06/10 07:40
#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耳机侦听事件
- QT显示机制
- NSNotificationCenter 的详细说明
- 传参方法:sharedApplication, NSUserDefaults, protocol 和 delegate(实例)
- CSDN 高校俱乐部学生主席 任务概要 笔记
- 《30天自制操作系统》读书笔记Day7
- iOS 如何监听出插入耳机 拔掉耳机事件?
- IOS项目常用代码
- 平面最近点对问题详解
- UITableView的多个UITextField时,有的被键盘挡住怎么解决
- iOS学习之分段Table View的使用(Grouped样式表格)
- Git SSH Key 生成步骤
- 关于解决打开多个文本聊窗口的端口占用问题的若干想法(官话~)
- UITableView 基本使用方法总结
- Linux 查看系统硬件信息(实例详解)