AudioSession property 详解及使用方法,包括检测是否有声音正在运行,音量更改等。


Posted on  2011年09月3日


enum { // typedef UInt32 AudioSessionPropertyID
kAudioSessionProperty_PreferredHardwareSampleRate = 'hwsr', // Float64 (get/set)
kAudioSessionProperty_PreferredHardwareIOBufferDuration = 'iobd', // Float32 (get/set)
kAudioSessionProperty_AudioCategory = 'acat', // UInt32 (get/set)
kAudioSessionProperty_AudioRoute = 'rout', // CFStringRef (get only)
kAudioSessionProperty_AudioRouteChange = 'roch', // CFDictionaryRef (property listener)
kAudioSessionProperty_CurrentHardwareSampleRate = 'chsr', // Float64 (get only)
kAudioSessionProperty_CurrentHardwareInputNumberChannels = 'chic', // UInt32 (get only)
kAudioSessionProperty_CurrentHardwareOutputNumberChannels = 'choc', // UInt32 (get only)
kAudioSessionProperty_CurrentHardwareOutputVolume = 'chov', // Float32 (get only/property listener)
kAudioSessionProperty_CurrentHardwareInputLatency = 'cilt', // Float32 (get only)
kAudioSessionProperty_CurrentHardwareOutputLatency = 'colt', // Float32 (get only)
kAudioSessionProperty_CurrentHardwareIOBufferDuration = 'chbd', // Float32 (get only)
kAudioSessionProperty_OtherAudioIsPlaying = 'othr', // UInt32 (get only)
kAudioSessionProperty_OverrideAudioRoute = 'ovrd', // UInt32 (set only)
kAudioSessionProperty_AudioInputAvailable = 'aiav', // UInt32 (get only/property listener)
kAudioSessionProperty_ServerDied = 'died', // UInt32 (property listener)
kAudioSessionProperty_OtherMixableAudioShouldDuck = 'duck', // UInt32 (get/set)
kAudioSessionProperty_OverrideCategoryMixWithOthers = 'cmix', // UInt32 (get, some set)
kAudioSessionProperty_OverrideCategoryDefaultToSpeaker = 'cspk', // UInt32 (get, some set)
kAudioSessionProperty_OverrideCategoryEnableBluetoothInput = 'cblu', // UInt32 (get, some set)
kAudioSessionProperty_InterruptionType = 'type', // UInt32 (get only)
};

//具体作用如果字面不理解可以查看AudioServices.h(相关设置值,返回值头文件中也有)

listener使用:
AudioSessionInitialize (NULL, NULL, NULL, NULL);//初始化
OSStatus state = AudioSessionAddPropertyListener(
kAudioSessionProperty_AudioRouteChange,
audioRouteChangeListenerCallback,self);
//state返回值如下
enum
{
kAudioSessionNoError = 0,
kAudioSessionNotInitialized = '!ini',
kAudioSessionAlreadyInitialized = 'init',
kAudioSessionInitializationError = 'ini?',
kAudioSessionUnsupportedPropertyError = 'pty?',
kAudioSessionBadPropertySizeError = '!siz',
kAudioSessionNotActiveError = '!act',
kAudioServicesNoHardwareError = 'nohw',
kAudioSessionNoCategorySet = '?cat',
kAudioSessionIncompatibleCategory = '!cat',
kAudioSessionUnspecifiedError = 'what'
};

//get的使用
UInt32 propertySize = sizeof(CFStringRef);
AudioSessionInitialize(NULL, NULL, NULL, NULL);
CFStringRef state = nil;
AudioSessionGetProperty(
kAudioSessionProperty_AudioRoute,
&propertySize,&state);

//set的使用
UInt32 propertySize = sizeof(UInt32);
AudioSessionInitialize(NULL, NULL, NULL, NULL);
UInt32 state = (1);
AudioSessionSetProperty(
kAudioSessionProperty_OtherMixableAudioShouldDuck,
propertySize,&state);


Posted in iPad, iPhone, Study Notes, 技巧 Tagged iPhone, objc, 开发 Leave a reply
Sep 03

检测iphone插入/拔出耳机事件

Posted on  2011年09月3日


void audioRouteChangeListenerCallback (
void *inUserData,
AudioSessionPropertyID inID,
UInt32 inDataSize,
const void *inData)
{
UInt32 propertySize = sizeof(CFStringRef);
AudioSessionInitialize(NULL, NULL, NULL, NULL);
CFStringRef state = nil;
AudioSessionGetProperty(kAudioSessionProperty_AudioRoute
,&propertySize,&state);

NSLog(@"%@",(NSString *)state);//return @"Headphone" or @"Speaker" and so on.
}
- (void)viewDidLoad {
[super viewDidLoad];

AudioSessionInitialize (NULL, NULL, NULL, NULL);
OSStatus status = AudioSessionAddPropertyListener(
kAudioSessionProperty_AudioRouteChange,
audioRouteChangeListenerCallback,self);
//if(status == 0){//ok;}
}

你可能感兴趣的:(AudioSession property 详解及使用方法,包括检测是否有声音正在运行,音量更改等。)