如何使用核心音频设置/获取音量级别?

3

我希望能够使用Core Audio获取和设置系统音量级别。我已经按照这个线程上的代码进行尝试: objective c audio meter

然而,我的调用AudioHardwareServiceHasProperty函数来查找kAudioHardwareServiceDeviceProperty_VirtualMasterVolume属性时返回false。为什么会发生这种情况,我该如何解决?采用什么方式可以获取和设置系统音量级别?

1个回答

4

你尝试过使用kAudioDevicePropertyVolumeScalar吗:

UInt32 channel = 1; // Channel 0  is master, if available
AudioObjectPropertyAddress prop = { 
    kAudioDevicePropertyVolumeScalar, 
    kAudioDevicePropertyScopeOutput,
    channel 
};

if(!AudioObjectHasProperty(deviceID, &prop))
    // error

Float32 volume;
UInt32 dataSize = sizeof(volume);
OSStatus result = AudioObjectGetPropertyData(deviceID, &prop, 0, NULL, &dataSize, &volume);

if(kAudioHardwareNoError != result)
    // error

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接