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

hou*_*oft 4 objective-c core-audio osx-mountain-lion

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

我发现了这个问题,在Snow Leopard中控制OS X卷,建议:

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);
Run Code Online (Sandbox Code Playgroud)

这对我没什么用,打印出来status is 2003332927.

我用值也试图20address结构,相同的结果兼而有之.

我如何解决这个问题并使其实际减少到50%?

Tho*_*ell 9

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

#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 ...
}
Run Code Online (Sandbox Code Playgroud)

然后,您可以在通道1(左)和通道2(右)上设置音量.注意,似乎不支持通道0(主)(set命令返回'who?')

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 ...
}
Run Code Online (Sandbox Code Playgroud)

希望这能回答你的问题!