简体   繁体   中英

Setting Mac OS X volume programmatically after 10.6 (Snow Leopard)

Is there a way to set the Mac's System Volume using Objective-C? I tried using:

AudioDeviceSetProperty([[self class]defaultOutputDeviceID],
                       NULL, //time stamp not needed
                       0, //channel 0 is master channel
                       false,  //for an output device
                       kAudioDevicePropertyVolumeScalar,
                       sizeof(Float32),
                       &volume);

But it is deprecated after OS X 10.6 (Snow Leopard); is there a better way to do this? Or will I have to settle for application volume?

Check out Audio Hardware Services in AudioToolbox:

Here is some sample code assuming the same context as your other code:

AudioObjectPropertyAddress propertyAddress = { 
    kAudioHardwareServiceDeviceProperty_VirtualMasterVolume, 
    kAudioDevicePropertyScopeOutput,
    kAudioObjectPropertyElementMaster 
};

AudioHardwareServiceSetPropertyData([self.class defaultOutputDeviceID], 
                                    &propertyAddress, 
                                    0, 
                                    NULL, 
                                    sizeof(Float32),
                                    &volume);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM