Controlling volume of running applications in Mac

2019-07-18 17:16发布

Please edvice by objective-c code snippets and useful links of how can I control all audio signals of output in OS X?

I think it should be something like proxy layer somewhere in OS X logic layers.

Thank you!

1条回答
太酷不给撩
2楼-- · 2019-07-18 18:01

It's somewhat sad that there is no simple API to do this. Luckily it isn't too hard, just verbose.

First, get the system output device:

UInt32 size;
AudioDeviceID outputDevice;
OSStatus result = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOuputDevice, &size, &outputDevice);

Then set the volume:

Float32 theVolume;
result = AudioDeviceSetProperty(theDevice, NULL, 0, /* master channel */ false, kAudioDevicePropertyVolumeScalar, sizeof(Float32), &theVolume);

Obviously I've omitted any error checking, which is a must.

Things can get a bit tricky because not all devices support channel 0 (the master channel). If this is the case with your device (it probably is) you have two options: query the device for its preferred stereo pair and set the volume on those channels individually, or just set the volume on channels 1 and 2.

查看更多
登录 后发表回答