使用 Swift 在 OS X 10.11 中设置 OS X 音量而不使用已弃用的 AudioHardwareServiceSetPropertyData API [英] Set OS X volume in OS X 10.11 using Swift without using the deprecated AudioHardwareServiceSetPropertyData API
问题描述
我找到了这段代码 使用 Swift 设置系统音量.在操场上使用代码时,我收到了警告.
I found this code to set the system volume using Swift. While using the code in a playground I got warning.
AudioHardwareServiceSetPropertyData 在 OS X 10.11 中被弃用
AudioHardwareServiceSetPropertyData was deprecated in OS X 10.11
如何更新 OS X 10.11 的代码?
How do I update the code for OS X 10.11?
谢谢.:)
推荐答案
正如 this 中所建议的回答一个类似的Objective-C问题,我建议使用ISSoundAdditions,你可以这样从 Swift 调用:
As also suggested in this answer to a similar Objective-C question, I'd suggest using ISSoundAdditions, which you can call from Swift as such:
NSSound.setSystemVolume(0.5)
-setSystemVolume
的实现 开始在 ISSoundAdditions.m 中的第 113 行 附近,以防您对它们如何完成设置系统音量感到好奇.
The implementation of -setSystemVolume
begins around line 113 in ISSoundAdditions.m in case you're curious of how they accomplish setting the system volume.
为了澄清对我的答案的事实质疑评论(显然已被删除),当使用 El Capitan SDK 进行编译时,ISSoundAdditions 中没有使用已弃用的 API – 未使用 AudioHardwareServiceSetPropertyData
.如果使用已弃用的 AudioHardwareServiceSetPropertyData
,AudioObjectSetPropertyData
确实是应该切换到的 API,但正如您从我链接到的 ISSoundAdditions 实现中看到的那样,涉及到一些工作(因此我首先链接到实现).
To clarify a factually challenged comment to my answer (since deleted, apparently), there is no use of deprecated APIs in ISSoundAdditions when compiling with the El Capitan SDK – AudioHardwareServiceSetPropertyData
is not used. AudioObjectSetPropertyData
is indeed the API one should switch to if using the deprecated AudioHardwareServiceSetPropertyData
, though as you can see from the ISSoundAdditions implementation I linked to, there's a bit of work involved (hence I linked to the implementation in the first place).
这篇关于使用 Swift 在 OS X 10.11 中设置 OS X 音量而不使用已弃用的 AudioHardwareServiceSetPropertyData API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!