例如:当我在OS X 10.7上使用Spotify.app并且出现声音广告时,我关闭了Mac的音量,应用程序将暂停广告,直到我将其恢复到平均水平.我发现这令人讨厌,并且违反了用户隐私,但Spotify已经找到了一种方法来做到这一点.
有什么办法可以用Cocoa做到这一点吗?我正在制作一个应用程序,如果音量很低,它可能会对用户发出警告.
解决方法 有两种选择.第一步是确定您想要的设备并获取其ID.假设默认输出设备,代码将类似于:AudioObjectPropertyAddress propertyAddress = { kAudioHarDWarePropertyDefaultOutputDevice,kAudioObjectPropertyScopeGlobal,kAudioObjectPropertyElementMaster };Audiodeviceid deviceid;UInt32 dataSize = sizeof(deviceid);Osstatus result = AudioObjectGetPropertyData(kAudioObjectSystemObject,&propertyAddress,NulL,&dataSize,&deviceid);if(kAudioHarDWareNoError != result) // Handle the error
接下来,您可以使用kAudioHarDWareServiceDeviceProperty_VirtualMasterVolume属性来获取设备的虚拟主卷:
AudioObjectPropertyAddress propertyAddress = { kAudioHarDWareServiceDeviceProperty_VirtualMasterVolume,kAudioDevicePropertyScopeOutput,kAudioObjectPropertyElementMaster };if(!AudioHarDWareServiceHasProperty(deviceid,&propertyAddress)) // An error occurredfloat32 volume;UInt32 dataSize = sizeof(volume);Osstatus result = AudioHarDWareServiceGetPropertyData(deviceid,&volume);if(kAudioHarDWareNoError != result) // An error occurred
或者,您可以使用kAudioDevicePropertyVolumeScalar获取特定频道的音量:
UInt32 channel = 1; // Channel 0 is master,if availableAudioObjectPropertyAddress propertyAddress = { kAudioDevicePropertyVolumeScalar,channel };if(!AudioObjectHasProperty(deviceid,&propertyAddress)) // An error occurredfloat32 volume;UInt32 dataSize = sizeof(volume);Osstatus result = AudioObjectGetPropertyData(deviceid,&volume);if(kAudioHarDWareNoError != result) // An error occurred
Apple的文档解释了两者之间的区别:
kAudioHarDWareServiceDeviceProperty_VirtualMasterVolume
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.
因此,准确定义设备的音量可能很棘手,尤其是对于具有非标准频道映射的多声道设备.
总结以上是内存溢出为你收集整理的objective-c – 如何获取计算机的当前音量?全部内容,希望文章能够帮你解决objective-c – 如何获取计算机的当前音量?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)