如何获得当前音量[重复]
Posted
技术标签:
【中文标题】如何获得当前音量[重复]【英文标题】:How to get Current Volume Level [duplicate] 【发布时间】:2013-02-27 16:40:56 【问题描述】:我正在开发 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】:有两种选择。第一步是确定您想要什么设备并获取其 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 音频对象关联的硬件设备。如果设备具有主音量控制,则此属性对其进行控制。如果设备具有单独的通道音量控件,则此属性适用于由设备的首选多通道布局标识的那些,或者如果设备只有立体声,则适用于首选立体声对。此控件在其影响的通道之间保持相对平衡。
因此,准确定义设备的音量可能很棘手,尤其是对于具有非标准通道映射的多通道设备。希望对你有帮助
【讨论】:
以上是关于如何获得当前音量[重复]的主要内容,如果未能解决你的问题,请参考以下文章
在 Python 中读取和/或更改 Windows 8 主卷 [重复]