gpt4 book ai didi

objective-c - 以编程方式更改 OS X 系统卷

转载 作者:太空狗 更新时间:2023-10-30 03:58:50 32 4
gpt4 key购买 nike

如何从 Objective-C 以编程方式更改音量?

我发现了这个问题, Controlling OS X volume in Snow Leopard 建议这样做:

Float32 volume = 0.5;
UInt32 size = sizeof(Float32);

AudioObjectPropertyAddress address = {
kAudioDevicePropertyVolumeScalar,
kAudioDevicePropertyScopeOutput,
1 // Use values 1 and 2 here, 0 (master) does not seem to work
};

OSStatus err;
err = AudioObjectSetPropertyData(kAudioObjectSystemObject, &address, 0, NULL, size, &volume);
NSLog(@"status is %i", err);

这对我没有任何作用,并打印出 status is 2003332927

我还尝试在 address 结构中使用值 20,两者的结果相同。

我该如何解决这个问题并让它实际将音量降低到 50%?

最佳答案

您需要先获取默认音频设备:

#import <CoreAudio/CoreAudio.h>

AudioObjectPropertyAddress getDefaultOutputDevicePropertyAddress = {
kAudioHardwarePropertyDefaultOutputDevice,
kAudioObjectPropertyScopeGlobal,
kAudioObjectPropertyElementMaster
};

AudioDeviceID defaultOutputDeviceID;
UInt32 volumedataSize = sizeof(defaultOutputDeviceID);
OSStatus result = AudioObjectGetPropertyData(kAudioObjectSystemObject,
&getDefaultOutputDevicePropertyAddress,
0, NULL,
&volumedataSize, &defaultOutputDeviceID);

if(kAudioHardwareNoError != result)
{
// ... handle error ...
}

然后您可以设置 channel 1(左)和 channel 2(右)的音量。请注意, channel 0(主)似乎不受支持(设置命令返回“谁?”)

AudioObjectPropertyAddress volumePropertyAddress = {
kAudioDevicePropertyVolumeScalar,
kAudioDevicePropertyScopeOutput,
1 /*LEFT_CHANNEL*/
};

Float32 volume;
volumedataSize = sizeof(volume);

result = AudioObjectSetPropertyData(defaultOutputDeviceID,
&volumePropertyAddress,
0, NULL,
sizeof(volume), &volume);
if (result != kAudioHardwareNoError) {
// ... handle error ...
}

希望这能回答您的问题!

关于objective-c - 以编程方式更改 OS X 系统卷,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17715111/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com