首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在macOS上,应用程序是否可以禁用/抑制所有本身没有发出的系统音频输出?

在macOS上,应用程序是否可以禁用/抑制所有本身没有发出的系统音频输出?
EN

Stack Overflow用户
提问于 2016-08-19 11:21:02
回答 1查看 897关注 0票数 4

在一个应用程序中,我使用macOS上连接的USB音频接口驱动激光投影设备。

激光装置以模拟音频作为输入。

作为一项安全功能,如果我能让我的应用程序的音频输出成为唯一的输出,那将是很棒的,因为来自其他应用程序或操作系统本身的任何其他音频,如果被路由到USB音频接口,都会与我的激光控制音频混合在一起,这是不必要的,也是潜在的安全隐患。

是否有可能在macOS上使我的应用程序的音频输出是独家的?我知道你可以在iOS上配置iOS来实现这一点(你可以避开其他应用程序的音频,但是通知声音反过来会使你的应用程序变慢),但是在Mac上这样的事情可能吗?它不需要与AppStore兼容。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-27 01:16:35

是的,您可以请求CoreAudio为您提供对音频输出设备的独占访问。这就是所谓的霸占装置。如果您占用了所有的设备,其他应用程序(包括系统)将无法发出任何声音。

像这样的东西对于单个设备来说是很有用的:

代码语言:javascript
运行
复制
AudioObjectPropertyAddress HOG_MODE_PROPERTY = { kAudioDevicePropertyHogMode, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
AudioDeviceID deviceId = // your audio device ID
pid_t hoggingProcess = -1; // -1 means attempt to acquire exclusive access
UInt32 size = sizeof(pid_t);

AudioObjectSetPropertyData(deviceId, &HOG_MODE_PROPERTY, 0, NULL, size, &hoggingProcess);
assert(hoggingProcess == getpid()); // check that you have exclusive access

Hog模式通过设置一个名为AudioObjectkAudioDevicePropertyHogMode属性来工作。如果设备未被占用,则属性值为-1。如果它被占用,则值是控制过程的进程id。

如果jump to definition on kAudioDevicePropertyHogMode in Xcode,则可以读取hog模式属性的标头文档。这是了解该属性(以及CoreAudio中的任何其他内容)如何工作的最佳方法。

为了完整起见,下面是头文档:

A pid\_t indicating the process that currently owns exclusive access to the AudioDevice or a value of -1 indicating that the device is currently available to all processes. If the AudioDevice is in a non-mixable mode, the HAL will automatically take hog mode on behalf of the first process to start an IOProc. Note that when setting this property, the value passed in is ignored. If another process owns exclusive access, that remains unchanged. If the current process owns exclusive access, it is released and made available to all processes again. If no process has exclusive access (meaning the current value is -1), this process gains ownership of exclusive access. On return, the pid\_t pointed to by inPropertyData will contain the new value of the property.

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39037794

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档