Marchenko Igor
Marchenko Igor

Reputation: 63

CoreAudio findout if audio device can change volume

At the moment I'm looking for way to find out if audio device can be volume configured.

I tried to use kAudioDevicePropertyVolumeRangeDecibels property to get range and if min and max values are equal than confirm as unchangeable volume. But unfortunately i couldn't get that value at all.

AudioObjectPropertyAddress addr;
addr.mSelector = kAudioDevicePropertyVolumeRangeDecibels;
addr.mScope = kAudioDevicePropertyScopeOutput;
addr.mElement = kAudioDevicePropertyScopeOutput;

UInt32 size;
AudioValueRange range;

OSStatus status = AudioObjectGetPropertyDataSize(self.audioDeviceID, &addr, 0, NULL, &size);
if (status != noErr)
{
    NSLog(@"error during size retrieval");
}else {
    status = AudioObjectGetPropertyData(self.audioDeviceID, &addr, 0, NULL, &size, &range);
    if (status != noErr)
    {
        NSLog(@"error during value retrieval");
    }
}

All the time i get error during size retrieval. (all other data as volume, channels count and so on is retrieved correctly).

Thanks for any solutions.

Upvotes: 0

Views: 508

Answers (1)

sbooth
sbooth

Reputation: 16986

I've accomplished this by checking whether the device supports the kAudioDevicePropertyVolumeScalar property. Some devices (but not many) support a master channel so first check kAudioObjectPropertyElementMaster. If this fails, check the individual channels you'd like to use:

AudioObjectPropertyAddress propertyAddress = { 
    kAudioDevicePropertyVolumeScalar, 
    kAudioDevicePropertyScopeOutput,
    kAudioObjectPropertyElementMaster 
};

if(AudioObjectHasProperty(deviceID, &propertyAddress))
    // YES

// Assume stereoChannels is a 2-deep array of the device's preferred stereo channels

propertyAddress.mElement = stereoChannels[0];
if(!AudioObjectHasProperty(deviceID, &propertyAddress))
    // NO

propertyAddress.mElement = stereoChannels[1];
if(!AudioObjectHasProperty(deviceID, &propertyAddress))
    // NO

// YES

Upvotes: 2

Related Questions