title: Controlling master volume on Mac through a Cocoa application
body: <p>I am looking to incorporate a NSSlider in a Cocoa application that controls the master volume of a Mac. I do not want to do this through AppleScript though, even though I know it is probably easier.</p>
  
  <p>I found this code that should be able to do the trick but does not because since 10.5 AudioDeviceGetProperty and its ilk have been deprecated. What needs to be changed to make this work?</p>
  
  <pre><code>- (float) getVolume 
  {
  float            b_vol;
  OSStatus        err;
  AudioDeviceID        device;
  UInt32            size;
  UInt32            channels[2];
  float            volume[2];
  
  // get device
  size = sizeof device;
  err = AudioObjectGetPropertyData(kAudioHardwarePropertyDefaultOutputDevice, &amp;device, &lt;#UInt32 inQualifierDataSize#&gt;, &lt;#const void *inQualifierData#&gt;, &amp;size, &lt;#void *outData#&gt;);
  if(err!=noErr) 
  {
      NSLog(@"audio-volume error get device");
      return 0.0;
  }
  
  // try set master volume (channel 0)
  size = sizeof b_vol;
  err = AudioDeviceGetProperty(device, 0, 0, kAudioDevicePropertyVolumeScalar, &amp;size, &amp;b_vol);    //kAudioDevicePropertyVolumeScalarToDecibels
  if(noErr==err) return b_vol;
  
  // otherwise, try seperate channels
  // get channel numbers
  size = sizeof(channels);
  err = AudioDeviceGetProperty(device, 0, 0,kAudioDevicePropertyPreferredChannelsForStereo, &amp;size,&amp;channels);
  if(err!=noErr) NSLog(@"error getting channel-numbers");
  
  size = sizeof(float);
  err = AudioDeviceGetProperty(device, channels[0], 0, kAudioDevicePropertyVolumeScalar, &amp;size, &amp;volume[0]);
  if(noErr!=err) NSLog(@"error getting volume of channel %d",channels[0]);
  err = AudioDeviceGetProperty(device, channels[1], 0, kAudioDevicePropertyVolumeScalar, &amp;size, &amp;volume[1]);
  if(noErr!=err) NSLog(@"error getting volume of channel %d",channels[1]);
  
  b_vol = (volume[0]+volume[1])/2.00;
  
  return  b_vol;
  }
  
  - (void)setVolume:(float)involume {
  OSStatus        err;
  AudioDeviceID        device;
  UInt32            size;
  Boolean            canset    = false;
  UInt32            channels[2];
  //float            volume[2];
  
  // get default device
  size = sizeof device;
  printf("setVolume:: value of the volume set =%lf", involume);
  err = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice, &amp;size, &amp;device);
  if(err!=noErr) {
      NSLog(@"audio-volume error get device");
      return;
  }
  
  
  // try set master-channel (0) volume
  size = sizeof canset;
  err = AudioDeviceGetPropertyInfo(device, 0, false, kAudioDevicePropertyVolumeScalar, &amp;size, &amp;canset);
  if(err==noErr &amp;&amp; canset==true) {
      size = sizeof involume;
      err = AudioDeviceSetProperty(device, NULL, 0, false, kAudioDevicePropertyVolumeScalar, size, &amp;involume);
      return;
  }
  
  // else, try seperate channes
  // get channels
  size = sizeof(channels);
  err = AudioDeviceGetProperty(device, 0, false, kAudioDevicePropertyPreferredChannelsForStereo, &amp;size,&amp;channels);
  if(err!=noErr) {
      NSLog(@"error getting channel-numbers");
      return;
  }
  
  // set volume
  size = sizeof(float);
  err = AudioDeviceSetProperty(device, 0, channels[0], false, kAudioDevicePropertyVolumeScalar, size, &amp;involume);
  if(noErr!=err) NSLog(@"error setting volume of channel %d",channels[0]);
  err = AudioDeviceSetProperty(device, 0, channels[1], false, kAudioDevicePropertyVolumeScalar, size, &amp;involume);
  if(noErr!=err) NSLog(@"error setting volume of channel %d",channels[1]);
  
  }
  </code></pre>
  
  <p>Thanks in advance!</p>