Eugenev
Eugenev

Reputation: 45

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

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!

Upvotes: 3

Views: 2286

Answers (1)

sbooth
sbooth

Reputation: 16966

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.

Upvotes: 4

Related Questions