静音系统音量是否会导致 coreadiuod 使用大量 CPU?
我使用以下代码将 OS X 系统音量静音并随后取消静音:
void SetMute(AudioDeviceID device, BOOL mute)
{
UInt32 muteVal = (UInt32)mute;
AudioObjectPropertyAddress address = {
kAudioDevicePropertyMute,
kAudioDevicePropertyScopeOutput,
0
};
OSStatus err;
err = AudioObjectSetPropertyData(device,
&address,
0,
NULL,
sizeof(UInt32),
&muteVal);
if (err)
{
NSString * message;
/* big switch statement on err to set message */
NSLog(@"error while %@muting: %@", (mute ? @"" : @"un"), message);
}
}
device
的值由以下代码确定
AudioDeviceID GetDefaultAudioDevice()
{
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;
}
我还检查系统音量当前是否静音,使用:
BOOL GetMute(AudioDeviceID device)
{
UInt32 size = sizeof(UInt32);
UInt32 muteVal;
AudioObjectPropertyAddress address = {
kAudioDevicePropertyMute,
kAudioDevicePropertyScopeOutput,
0
};
OSStatus err;
err = AudioObjectGetPropertyData(device,
&address,
0,
NULL,
&size,
&muteVal);
if (err)
{
NSString * message;
/* big switch to set message */
NSLog(@"error while getting mute status: %@", message);
}
return (BOOL)muteVal;
}
所有这些似乎工作正常-- 系统静音和取消静音的行为与用户按下键盘上的静音键相同(取消静音会恢复静音之前设置的最后音量等)。
我注意到,在我的程序执行此操作后的某个未指定时间,coreaudiod
开始消耗比正常情况更多的 CPU(在我的情况下,这仅为 5-7% 左右,但通常它报告 0.0 % 在活动监视器中)。据我所知,我在那里正确使用了 Core Audio API,并且看起来没有任何东西会导致 coreaudiod
中出现任何故障,所以我希望有人可以指出一个我的代码有问题,或者指出其他什么(可能不相关?)可能导致 coreaudiod 中的 CPU 使用率。我还应该注意到,当我观察到 coreaudiod 使用比正常情况更多的 CPU 时,没有声音播放,并且系统未静音。
I mute and later unmute the OS X system volume with the following code:
void SetMute(AudioDeviceID device, BOOL mute)
{
UInt32 muteVal = (UInt32)mute;
AudioObjectPropertyAddress address = {
kAudioDevicePropertyMute,
kAudioDevicePropertyScopeOutput,
0
};
OSStatus err;
err = AudioObjectSetPropertyData(device,
&address,
0,
NULL,
sizeof(UInt32),
&muteVal);
if (err)
{
NSString * message;
/* big switch statement on err to set message */
NSLog(@"error while %@muting: %@", (mute ? @"" : @"un"), message);
}
}
The value of device
is determined with
AudioDeviceID GetDefaultAudioDevice()
{
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;
}
And I also check whether the system volume is currently muted, with:
BOOL GetMute(AudioDeviceID device)
{
UInt32 size = sizeof(UInt32);
UInt32 muteVal;
AudioObjectPropertyAddress address = {
kAudioDevicePropertyMute,
kAudioDevicePropertyScopeOutput,
0
};
OSStatus err;
err = AudioObjectGetPropertyData(device,
&address,
0,
NULL,
&size,
&muteVal);
if (err)
{
NSString * message;
/* big switch to set message */
NSLog(@"error while getting mute status: %@", message);
}
return (BOOL)muteVal;
}
All of this seems to work fine -- the system mutes and unmutes with the same behavior as if the user hit the mute key on the keyboard (unmuting restores the last volume set before muting, etc).
I have noticed that at some unspecified time after my program does this, coreaudiod
begins to consume a lot more CPU than normal (in my case, this is only around 5-7%, but ordinarily it reports 0.0% in Activity Monitor). As far as I can tell, I'm using the Core Audio APIs correctly there, and nothing looks like it should be causing any glitches in coreaudiod
, so I'm hoping that someone can either point out an issue with my code, or point me towards what else (possibly unrelated?) might be causing the CPU usage in coreaudiod
. I should also note that when I observe coreaudiod
using more CPU than normal, there's no sound playing, and the system is unmuted.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论