views:

335

answers:

0

Unable to set/get headset mic volumeHi.

I am using the following method to retrieve the Mic volume, but AudioHardwareServiceHasProperty is failing. I realized that my mic has one input channel than the master. But the property, kAudioHardwareServiceDeviceProperty_VirtualMasterVolume documentation says irrespective of the way the volume channels, it shall give me the aggregate volume level. Can anybody help me what is wrong in the following code?

(float)volumeOfDeviceId:(AudioDeviceID)deviceId isInput:(BOOL)isInput
{
    NSLog(@"\n\nVolumeOfDeviceId:[%x] isInput:[%d]", deviceId, isInput);
    Float32            outputVolume;

    UInt32 propertySize = 0;
    OSStatus status = noErr;
    AudioObjectPropertyAddress propertyAOPA;
    propertyAOPA.mElement = kAudioObjectPropertyElementMaster;
    propertyAOPA.mSelector = kAudioHardwareServiceDeviceProperty_VirtualMasterVolume;
    propertyAOPA.mScope = (isInput? kAudioDevicePropertyScopeInput: kAudioDevicePropertyScopeOutput);

    AudioDeviceID outputDeviceID = deviceId;//[KNVolumeControl defaultOutputDeviceID];

    if (outputDeviceID == kAudioObjectUnknown)
    {
        NSLog(@"Unknown device");
        return 0.0;
    }

    if (!AudioHardwareServiceHasProperty(outputDeviceID, &propertyAOPA))
    {
        NSLog(@"ServiceFailed: No volume returned for device 0x%0x", outputDeviceID);
        return 0.0;
    }

    propertySize = sizeof(Float32);

    status = AudioHardwareServiceGetPropertyData(outputDeviceID, &propertyAOPA, 0, NULL, &propertySize, &outputVolume);

    if (status)
    {
        NSLog(@"PropertyData failed: No volume returned for device 0x%0x", outputDeviceID);
        return 0.0;
    }

    if (outputVolume < 0.0 || outputVolume > 1.0) return 0.0;

    return outputVolume;
}