ios监听输出设备变化(监听耳机插拔,蓝牙设备连接断开等)的实现

 在ios6以前,我们有如下的方法:

#import<AVFoundation/AVFoundation.h>

 

    [[AVAudioSession sharedInstance] setDelegate:self];


  AudioSessionAddPropertyListener(kAudioSessionProperty_AudioRouteChange,audioRouteChangeListenerCallback, self);

然后实现该回调:

//音频监控回调函数

static void audioRouteChangeListenerCallback (void                      *inUserData,

                                              AudioSessionPropertyID    inPropertyID,

                                              UInt32                    inPropertyValueSize,

                                              constvoid                *inPropertyValue

                                              )

{

    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 = inPropertyValue;

    CFNumberRef     routeChangeReasonRef = CFDictionaryGetValue (routeChangeDictionary, CFSTR (kAudioSession_AudioRouteChangeKey_Reason));

    SInt32          routeChangeReason;

    CFNumberGetValue (routeChangeReasonRef,kCFNumberSInt32Type, &routeChangeReason);

//  do your handling here

}

请注意 [[ AVAudioSession  sharedInstance setDelegate : self ]一定不要遗漏,否则该回调应该无法触发。

------------------------分割线------------------------
上面的方法是ios6以前的实现方式,我们可以看出这个api是比较低级的实现,其回调还是c的实现方式,而不是我们平常习惯的oc实现。
因此在ios6及以后,上面的api被deprecated了(当然,你要是还这么用,也还是能够实现功能),我们有更好更高级的实现来解决问题:

    [[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(outputDeviceChanged:)name:AVAudioSessionRouteChangeNotificationobject:[AVAudioSessionsharedInstance]];

- (void)outputDeviceChanged:(NSNotification *)aNotification

{

 // do your jobs here

}

请注意,addobserver的参数填写:其中的object必须是 [ AVAudioSession   sharedInstance ],而不是我们通常很多情况下填写的nil,此处若为nil,通知也不会触发。







  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值