Controlling volume of running applications in Mac OS X via Objective-C

左心房为你撑大大i 提交于 2019-12-24 00:38:06

问题


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:


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.



来源:https://stackoverflow.com/questions/5109971/controlling-volume-of-running-applications-in-mac-os-x-via-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!