0
我在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未能獲得當前的卷屬性,任何想法是怎麼回事錯,
這是選擇默認的輸出設備的代碼,
-(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;
}