Set OS X volume in OS X 10.11 using Swift without using the deprecated AudioHardwareServiceSetPropertyData API

对着背影说爱祢 提交于 2019-12-19 10:49:07

问题


I found this code to set the system volume using Swift. While using the code in a playground I got warning.

AudioHardwareServiceSetPropertyData was deprecated in OS X 10.11

How do I update the code for OS X 10.11?

Thanks. :)


回答1:


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)

The implementation of -setSystemVolume begins around line 113 in ISSoundAdditions.m in case you're curious of how they accomplish setting the system volume.

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).



来源:https://stackoverflow.com/questions/36953948/set-os-x-volume-in-os-x-10-11-using-swift-without-using-the-deprecated-audiohard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!