如何获得当前音量水平

时间:2013-02-27 16:40:56

标签: objective-c macos audio core-audio

我正在研究10.8

这是获取当前音量音量的代码,

-(float)getVolume{

    float volume = 0.0;

    UInt32 thePropSize = sizeof(volume);

    AudioDeviceID devId = [self GetOutputAudioDevice];

    AudioObjectPropertyAddress thePropertyAddress = { kAudioDevicePropertyVolumeScalar, kAudioDevicePropertyScopeOutput, kAudioObjectPropertyElementMaster };


    if(AudioObjectHasProperty(devId, &thePropertyAddress)){
        AudioObjectGetPropertyData(devId, &thePropertyAddress, 0, NULL, &thePropSize, &volume);
    }else{
        printf(" doesn't have property to get the volume");
    }

    return volume;
}

函数 AudioObjectHasProperty 无法获取Current Vol属性,知道出了什么问题,

这是选择默认输出设备的代码,

-(AudioDeviceID)GetOutputAudioDevice{

    OSStatus err;
    AudioDeviceID device = 0;
    UInt32 size = sizeof(AudioDeviceID);
    AudioObjectPropertyAddress address = {
        kAudioHardwarePropertyDefaultOutputDevice,
        kAudioObjectPropertyScopeGlobal,
        kAudioObjectPropertyElementMaster
    };

    err = AudioObjectGetPropertyData(kAudioObjectSystemObject,
                                     &address,
                                     0,
                                     NULL,
                                     &size,
                                     &device);
    if (err)
    {
        NSLog(@"could not get default audio output device");
    }

    return device;
}

1 个答案:

答案 0 :(得分:1)

有两种选择。第一步是确定您想要的设备并获取其ID。假设默认输出设备,代码将类似于:

AudioObjectPropertyAddress propertyAddress = { 
    kAudioHardwarePropertyDefaultOutputDevice, 
    kAudioObjectPropertyScopeGlobal, 
    kAudioObjectPropertyElementMaster 
};

AudioDeviceID deviceID;
UInt32 dataSize = sizeof(deviceID);
OSStatus result = AudioObjectGetPropertyData(kAudioObjectSystemObject, &propertyAddress, 0, NULL, &dataSize, &deviceID);

if(kAudioHardwareNoError != result)
    // Handle the error
Next, you can use the kAudioHardwareServiceDeviceProperty_VirtualMasterVolume property to get the device's virtual master volume:

AudioObjectPropertyAddress propertyAddress = { 
    kAudioHardwareServiceDeviceProperty_VirtualMasterVolume, 
    kAudioDevicePropertyScopeOutput,
    kAudioObjectPropertyElementMaster 
};

if(!AudioHardwareServiceHasProperty(deviceID, &propertyAddress))
    // An error occurred

Float32 volume;
UInt32 dataSize = sizeof(volume);
OSStatus result = AudioHardwareServiceGetPropertyData(deviceID, &propertyAddress, 0, NULL, &dataSize, &volume);

if(kAudioHardwareNoError != result)
    // An error occurred
Alternatively, you can use kAudioDevicePropertyVolumeScalar to get the volume for a specific channel:

UInt32 channel = 1; // Channel 0  is master, if available
AudioObjectPropertyAddress propertyAddress = { 
    kAudioDevicePropertyVolumeScalar, 
    kAudioDevicePropertyScopeOutput,
    channel 
};

if(!AudioObjectHasProperty(deviceID, &propertyAddress))
    // An error occurred

Float32 volume;
UInt32 dataSize = sizeof(volume);
OSStatus result = AudioObjectGetPropertyData(deviceID, &propertyAddress, 0, NULL, &dataSize, &volume);

if(kAudioHardwareNoError != result)
    // An error occurred

Apple的文档中解释了两者之间的区别:

kAudioHardwareServiceDeviceProperty_VirtualMasterVolume

表示音量控件值的Float32值。此属性值的范围是0.0(静音)到1.0(完整级别)。此属性的效果取决于与HAL音频对象关联的硬件设备。如果设备具有主音量控制,则此属性对其进行控制。如果设备具有单独的通道音量控制,则此属性适用于由设备首选多声道布局标识的设备,或者如果设备仅为立体声设备,则适用于首选立体声对。该控件保持其影响的通道之间的相对平衡。

因此,准确定义设备的音量是非常棘手的,特别是对于具有非标准频道映射的多声道设备。我希望有帮助