日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

IOS成长之路-检测耳机插入/拔出

發布時間:2024/9/30 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 IOS成长之路-检测耳机插入/拔出 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

導入蘋果的兩個框架是必不可少的環節。。。



代碼部分+小解:

[cpp]?view plaincopy
  • -?(void)viewDidLoad??
  • {??
  • ????[super?viewDidLoad];??
  • ????//?Do?any?additional?setup?after?loading?the?view,?typically?from?a?nib.??
  • ????AudioSessionInitialize?(NULL,?NULL,?NULL,?NULL);??
  • ????/*?
  • ????????OSStatus?AudioSessionInitialize?(?
  • ????????????CFRunLoopRef??????????????????????inRunLoop,?
  • ????????????CFStringRef???????????????????????inRunLoopMode,?
  • ????????????AudioSessionInterruptionListener??inInterruptionListener,?
  • ????????????void??????????????????????????????*inClientData?
  • ????????);?
  • ?????這個函數,必須在調用其他AudioSession?functions之前調用?
  • ??????
  • ?????inRunLoop?
  • ?????The?run?loop?that?the?interruption?listener?callback?should?be?run?on.?Pass?NULL?to?use?the?main?run?loop.?
  • ?????置?NULL?,是使用默認的the?main?run?loop;(當在監聽器回調的時候停止循環)?
  • ??????
  • ?????inRunLoopMode?
  • ?????The?mode?for?the?run?loop?that?the?interruption?listener?function?will?run?on.?Passing?NULL?is?equivalent?to?passing?kCFRunLoopDefaultMode(kCFRunLoopDefaultMode來持有對象,在應用或線程閑置的時候這些對象被監控).?
  • ?????(當監聽器將要回調的時候運行循環中斷)??NULL?==?kCFRunLoopDefaultMode,?
  • ??????
  • ?????inInterruptionListener?
  • ?????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.?
  • ?????用?NULL?來代替?AudioSessionInterruptionListener(音頻會話被打斷),當我們拔下耳機的時候,音頻會話被打斷,從而使得應用程序的音頻對象引起了回調。?
  • ??????
  • ?????inClientData?
  • ?????Data?that?you?would?like?to?be?passed?to?your?interruption?listener?callback.?
  • ?????*/??
  • ??????
  • ??????
  • ????[self?addHeadPhoneListener];??
  • }??

  • 添加監聽事件和回調函數:

    [cpp]?view plaincopy
  • //監聽耳機插入和拔出??
  • -?(BOOL)addHeadPhoneListener??
  • {??
  • ????OSStatus?status?=?AudioSessionAddPropertyListener(??
  • ??????????????????????????????????????????????????????kAudioSessionProperty_AudioRouteChange,??
  • ??????????????????????????????????????????????????????audioRouteChangeListenerCallback,self);??
  • ????/*?
  • ?????AudioSessionAddPropertyListener(?
  • ?????AudioSessionPropertyID??????????????inID,?
  • ?????AudioSessionPropertyListener????????inProc,?
  • ?????void????????????????????????????????*inClientData?
  • ?????)?
  • ?????注冊一個監聽:audioRouteChangeListenerCallback,當音頻會話傳遞的方式(耳機/喇叭)發生改變的時候,會觸發這個監聽?
  • ?????kAudioSessionProperty_AudioRouteChange?:就是檢測音頻路線是否改變?
  • ?????*/??
  • }??
  • void?audioRouteChangeListenerCallback?(??
  • ???????????????????????????????????????void??????????????????????*inUserData,??
  • ???????????????????????????????????????AudioSessionPropertyID????inPropertyID,??
  • ???????????????????????????????????????UInt32????????????????????inPropertyValueS,??
  • ???????????????????????????????????????const?void????????????????*inPropertyValue??
  • ???????????????????????????????????????)?{??
  • ????UInt32?propertySize?=?sizeof(CFStringRef);??
  • ????AudioSessionInitialize(NULL,?NULL,?NULL,?NULL);??
  • ????CFStringRef?state?=?nil;??
  • ??????
  • ????//獲取音頻路線??
  • ????AudioSessionGetProperty(kAudioSessionProperty_AudioRoute??
  • ????????????????????????????,&propertySize,&state);//kAudioSessionProperty_AudioRoute:音頻路線??
  • ????NSLog(@"%@",(NSString?*)state);//Headphone?耳機??Speaker?喇叭.??
  • }??

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

    總結

    以上是生活随笔為你收集整理的IOS成长之路-检测耳机插入/拔出的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。