iOS检测耳机插入/拔出

来源:互联网 发布:java new jsonobject 编辑:程序博客网 时间:2024/05/01 23:38

导入苹果的两个框架是必不可少的环节。。。



代码部分+小解:

[cpp] view plain copy
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     // Do any additional setup after loading the view, typically from a nib.  
  5.     AudioSessionInitialize (NULL, NULL, NULL, NULL);  
  6.     /* 
  7.         OSStatus AudioSessionInitialize ( 
  8.             CFRunLoopRef                      inRunLoop, 
  9.             CFStringRef                       inRunLoopMode, 
  10.             AudioSessionInterruptionListener  inInterruptionListener, 
  11.             void                              *inClientData 
  12.         ); 
  13.      这个函数,必须在调用其他AudioSession functions之前调用 
  14.       
  15.      inRunLoop 
  16.      The run loop that the interruption listener callback should be run on. Pass NULL to use the main run loop. 
  17.      置 NULL ,是使用默认的the main run loop;(当在监听器回调的时候停止循环) 
  18.       
  19.      inRunLoopMode 
  20.      The mode for the run loop that the interruption listener function will run on. Passing NULL is equivalent to passing kCFRunLoopDefaultMode(kCFRunLoopDefaultMode来持有对象,在应用或线程闲置的时候这些对象被监控). 
  21.      (当监听器将要回调的时候运行循环中断)  NULL == kCFRunLoopDefaultMode, 
  22.       
  23.      inInterruptionListener 
  24.      The interruption listener callback function. The application’s audio session object invokes the callback when the session is interrupted and (if the application is still running) when the interruption ends. Can be NULL. See AudioSessionInterruptionListener. 
  25.      用 NULL 来代替 AudioSessionInterruptionListener(音频会话被打断),当我们拔下耳机的时候,音频会话被打断,从而使得应用程序的音频对象引起了回调。 
  26.       
  27.      inClientData 
  28.      Data that you would like to be passed to your interruption listener callback. 
  29.      */  
  30.       
  31.       
  32.     [self addHeadPhoneListener];  
  33. }  

添加监听事件和回调函数:

[cpp] view plain copy
  1. //监听耳机插入和拔出  
  2. - (BOOL)addHeadPhoneListener  
  3. {  
  4.     OSStatus status = AudioSessionAddPropertyListener(  
  5.                                                       kAudioSessionProperty_AudioRouteChange,  
  6.                                                       audioRouteChangeListenerCallback,self);  
  7.     /* 
  8.      AudioSessionAddPropertyListener( 
  9.      AudioSessionPropertyID              inID, 
  10.      AudioSessionPropertyListener        inProc, 
  11.      void                                *inClientData 
  12.      ) 
  13.      注册一个监听:audioRouteChangeListenerCallback,当音频会话传递的方式(耳机/喇叭)发生改变的时候,会触发这个监听 
  14.      kAudioSessionProperty_AudioRouteChange :就是检测音频路线是否改变 
  15.      */  
  16. }  
  17. void audioRouteChangeListenerCallback (  
  18.                                        void                      *inUserData,  
  19.                                        AudioSessionPropertyID    inPropertyID,  
  20.                                        UInt32                    inPropertyValueS,  
  21.                                        const void                *inPropertyValue  
  22.                                        ) {  
  23.     UInt32 propertySize = sizeof(CFStringRef);  
  24.     AudioSessionInitialize(NULL, NULL, NULL, NULL);  
  25.     CFStringRef state = nil;  
  26.       
  27.     //获取音频路线  
  28.     AudioSessionGetProperty(kAudioSessionProperty_AudioRoute  
  29.                             ,&propertySize,&state);//kAudioSessionProperty_AudioRoute:音频路线  
  30.     NSLog(@"%@",(NSString *)state);//Headphone 耳机  Speaker 喇叭.  
  31. }  

理解的不透彻,望各位大神指教。


0 0