2013-02-27 109 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音頻對象關聯的硬件設備。如果設備具有主音量控制,則此屬性將控制它。如果設備具有單獨的通道音量控制,則此屬性適用於由設備的首選多通道佈局標識的設備,或者如果設備僅爲立體聲,則適用於首選立體聲對。此控件可維持其影響的頻道之間的相對平衡。

因此,精確定義設備的音量是非常棘手的,特別是對於具有非標準通道映射的多聲道設備。我希望這可以幫助