我正在工作 10.8
这是获取当前扬声器音量的代码,
-(float)getVolume{
float volume = 0.0;
UInt32 thePropSize = sizeof(volume);
AudioDeviceID devId = [self GetOutputAudioDevice];
AudioObjectPropertyAddress thePropertyAddress = { kAudioDevicePropertyVolumeScalar, kAudioDevicePropertyScopeOutput, kAudioObjectPropertyElementMaster };
if(AudioObjectHasProperty(devId, &thePropertyAddress)){
AudioObjectGetPropertyData(devId, &thePropertyAddress, 0, NULL, &thePropSize, &volume);
}else{
printf(" doesn't have property to get the volume");
}
return volume;
}
函数AudioObjectHasProperty无法获取 Current Vol 属性,知道出了什么问题,
这是选择默认输出设备的代码,
-(AudioDeviceID)GetOutputAudioDevice{
OSStatus err;
AudioDeviceID device = 0;
UInt32 size = sizeof(AudioDeviceID);
AudioObjectPropertyAddress address = {
kAudioHardwarePropertyDefaultOutputDevice,
kAudioObjectPropertyScopeGlobal,
kAudioObjectPropertyElementMaster
};
err = AudioObjectGetPropertyData(kAudioObjectSystemObject,
&address,
0,
NULL,
&size,
&device);
if (err)
{
NSLog(@"could not get default audio output device");
}
return device;
}