0

我正在工作 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;
}
4

1 回答 1

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 音频对象关联的硬件设备。如果设备具有主音量控制,则此属性对其进行控制。如果设备具有单独的通道音量控件,则此属性适用于由设备的首选多通道布局标识的那些,或者如果设备仅为立体声,则适用于首选立体声对。此控件保持其影响的通道之间的相对平衡。

因此,准确定义设备的音量可能很棘手,尤其是对于具有非标准通道映射的多通道设备。我希望这会有所帮助

于 2013-02-27T16:53:30.940 回答