Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7764359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:53:27+00:00 2026-06-01T14:53:27+00:00

I am looking to incorporate a NSSlider in a Cocoa application that controls the

  • 0

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.

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?

- (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, &device, <#UInt32 inQualifierDataSize#>, <#const void *inQualifierData#>, &size, <#void *outData#>);
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, &size, &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, &size,&channels);
if(err!=noErr) NSLog(@"error getting channel-numbers");

size = sizeof(float);
err = AudioDeviceGetProperty(device, channels[0], 0, kAudioDevicePropertyVolumeScalar, &size, &volume[0]);
if(noErr!=err) NSLog(@"error getting volume of channel %d",channels[0]);
err = AudioDeviceGetProperty(device, channels[1], 0, kAudioDevicePropertyVolumeScalar, &size, &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, &size, &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, &size, &canset);
if(err==noErr && canset==true) {
    size = sizeof involume;
    err = AudioDeviceSetProperty(device, NULL, 0, false, kAudioDevicePropertyVolumeScalar, size, &involume);
    return;
}

// else, try seperate channes
// get channels
size = sizeof(channels);
err = AudioDeviceGetProperty(device, 0, false, kAudioDevicePropertyPreferredChannelsForStereo, &size,&channels);
if(err!=noErr) {
    NSLog(@"error getting channel-numbers");
    return;
}

// set volume
size = sizeof(float);
err = AudioDeviceSetProperty(device, 0, channels[0], false, kAudioDevicePropertyVolumeScalar, size, &involume);
if(noErr!=err) NSLog(@"error setting volume of channel %d",channels[0]);
err = AudioDeviceSetProperty(device, 0, channels[1], false, kAudioDevicePropertyVolumeScalar, size, &involume);
if(noErr!=err) NSLog(@"error setting volume of channel %d",channels[1]);

}

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T14:53:28+00:00Added an answer on June 1, 2026 at 2:53 pm

    This will do the trick – I just copied and pasted it out of one of my projects, so there are a few extraneous variables and functions but you should be able to get the gist of it:

    BOOL gotMaster = NO;
    BOOL gotLeft = NO;
    BOOL gotRight = NO;
    float volume = 1, lvolume = 1, rvolume = 1;
    float inVolume = 1, inLvolume = 1, inRvolume = 1;
    OSStatus result = noErr;
    AudioObjectPropertyAddress thePropertyAddress;
    UInt32 thePropSize = sizeof(Float32);
    thePropertyAddress.mSelector = kAudioDevicePropertyVolumeScalar;
    thePropertyAddress.mScope = kAudioDevicePropertyScopeOutput;    
    thePropertyAddress.mElement = kAudioObjectPropertyElementMaster;
    // see if the device supports volume control, if so, then get it
    if (AudioObjectHasProperty(_targetDevice, &thePropertyAddress)) {
      result = AudioObjectGetPropertyData(_targetDevice, &thePropertyAddress, 0, NULL, &thePropSize, &volume);
      if (result) { cwlog("AudioDeviceManager: Error in AudioObjectGetPropertyData: %d", result); return -1; }
      gotMaster = YES;
    } else {
      cwlog("AudioDeviceManager: The target device does not support getting the master volume");
    }
    // see if the device supports left channel volume control, if so, then get it
    thePropertyAddress.mElement = 1;
    if (AudioObjectHasProperty(_targetDevice, &thePropertyAddress)) {
      result = AudioObjectGetPropertyData(_targetDevice, &thePropertyAddress, 0, NULL, &thePropSize, &lvolume);
      if (result) { cwlog("AudioDeviceManager: Error in AudioObjectGetPropertyData: %d", result); return -1; }
      gotLeft = YES;
    } else {
      cwlog("AudioDeviceManager: The target device does not support getting the left channel volume");
    }
    thePropertyAddress.mElement = 2;
    // see if the device supports right channel volume control, if so, then get it
    if (AudioObjectHasProperty(_targetDevice, &thePropertyAddress)) {
      result = AudioObjectGetPropertyData(_targetDevice, &thePropertyAddress, 0, NULL, &thePropSize, &rvolume);
      if (result) { cwlog("AudioDeviceManager: Error in AudioObjectGetPropertyData: %d", result); return -1; }
      gotRight = YES;
    } else {
      // if the device does not support master volume control, do nothing
      cwlog("AudioDeviceManager: The target device does not support getting the right channel volume");
    }
    if(!gotMaster && !gotLeft && !gotRight) {
      cwlog("AudioDeviceManager: Could not get any volume settings for %s (%u)", [[self nameOfDevice:_targetDevice] UTF8String], _targetDevice);
      return -1;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking to incorporate a loop in R which goes through every game's
Picture this: you are creating a little module that people can incorporate into their
Im looking to incorporate a check functionality in TFS to ensure that new files
I'm looking on how to incorporate silverlight in my existing application and I keep
I 'm looking for Desktop sharing utility that I can use to incorporate inside
I'm looking for a working sample of an ASP.NET MVC web application that uses
I'd like to incorporate OpenSSH support into a Windows application and I am looking
I'm looking for tips and ideas on how to best incorporate authentication with PHP
I am looking for a 3rd party licensing module that can be incorporated into
Looking at some assembly code for x86_64 on my Mac, I see the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.