问题描述
如何从Cocoa API访问Mac的当前音量?
How do I access the current volume level of a Mac from the Cocoa API?
例如:当我在OS X 10.7上使用Spotify.app时,一个声音广告出来,我调低我的Mac的音量,应用程序将暂停广告,直到我把它恢复到一个平均水平。我发现这令人难以置信的讨厌,违反了用户隐私,但不知何故Spotify找到了一种方法来做到这一点。
For example: when I'm using Spotify.app on OS X 10.7 and a sound advertisement comes up, and I turn down my Mac's volume, the app will pause the ad until I turn it back up to an average level. I find this incredibly obnoxious and a violation of user privacy, but somehow Spotify has found a way to do this.
有什么办法我可以做这个Cocoa?我在做一个应用程序,它可能会有用的警告用户,如果音量低。
Is there any way I can do this with Cocoa? I'm making an app where it might come in useful to warn the user if the volume is low.
推荐答案
有两个选项可用。第一步是确定您想要的设备,并获取其ID。假设默认的输出设备,代码看起来像:
There are two options available. The first step is to determine what device you'd like and get its ID. Assuming the default output device, the code will look something like:
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
接下来,您可以使用 kAudioHardwareServiceDeviceProperty_VirtualMasterVolume
属性来获取设备的虚拟主卷:
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
或者,您可以使用 kAudioDevicePropertyVolumeScalar
获取特定频道的音量:
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的文档中解释了两者之间的区别:
The difference between the two is explained in Apple's docs:
Float32值,表示音量控制的值。该属性值的
范围是0.0(静默)到1.0(满
级别)。此属性的效果取决于与HAL音频对象相关联的硬件设备
。如果设备有一个master
音量控制,该属性控制它。如果设备具有
个单独通道音量控制,则该属性适用于由设备的优选多通道布局标识的那些
,或者如果设备仅是立体声的,则适用于
优选立体声对。这个控制
在它所影响的频道之间保持相对平衡。
A Float32 value that represents the value of the volume control. The range for this property’s value is 0.0 (silence) through 1.0 (full level). The effect of this property depends on the hardware device associated with the HAL audio object. If the device has a master volume control, this property controls it. If the device has individual channel volume controls, this property applies to those identified by the device's preferred multichannel layout, or the preferred stereo pair if the device is stereo only. This control maintains relative balance between the channels it affects.
因此,定义设备的音量是,特别是对于具有非标准频道地图的多频道设备。
So it can be tricky to define exactly what a device's volume is, especially for multichannel devices with non-standard channel maps.
这篇关于如何获取计算机的当前音量级别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!