2013-02-27 106 views
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未能获得当前的卷属性,任何想法是怎么回事错,

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

-(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

有两个选项可用。第一步是确定你想要什么设备并获得它的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 

两者之间的区别在苹果的文档说明:

kAudioHardwareServiceDeviceProperty_VirtualMasterVolume

表示音量的值

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

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