ios监听输出设备变化(监听耳机插拔,蓝牙设备连接断开等)的实现
2014-10-30 15:01
573 查看
在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,通知也不会触发。
#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,通知也不会触发。
相关文章推荐
- ios监听输出设备变化
- ios蓝牙开发(三)ios连接外设的代码实现:手机app去读写蓝牙设备。
- IOS app蓝牙连接硬件设备 自动断开问题
- iOS监听耳机插拔的不使用系统通知实现的一种方式
- iOS 蓝牙开发(二)iOS 连接外设的代码实现
- ios蓝牙开发(二)ios连接外设的代码实现
- Android 蓝牙( Bluetooth)耳机连接分析及实现
- 【投稿】iOS 蓝牙开发(二)iOS 连接外设的代码实现
- iOS 蓝牙开发(三)app作为外设被连接的实现
- 《在WinCE5中编程查找并连接周围的蓝牙设备并实现通信》一文配套原码的修正
- IOS 蓝牙相关-app作为外设被连接的实现(3)
- Android 蓝牙( Bluetooth)耳机连接分析及实现
- iOS 设备 检测声音输出设备及耳机麦克风的处理
- Android如何监听蓝牙耳机和有线耳机的插拔
- iOS 蓝牙开发(二)iOS 连接外设的代码实现(转)
- IOS 蓝牙相关-连接外设的代码实现(2)
- iOS8 蓝牙设备的重连接(retrieve) Swift实现
- Android 蓝牙( Bluetooth)耳机连接分析及实现
- Android利用广播监听设备网络连接(断网)的变化情况
- 关于蓝牙设备与ios连接后,自动打开一个app