Reputation:
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. :)
Upvotes: 3
Views: 1248
Reputation: 199
I cannot yet comment, hence I post this as a separate answer. I have updated the great ISSoundAdditions to true Swift 5 and gave it a nice public interface, through which you manage the system volume by getting and setting a class property of NSSound– examples:
NSSound.systemVolume += 0.2
NSSound.systemVolumeIsMuted = true
As a little goodie, you can mute the system volume fading out smoothly (and non-blocking in the background):
NSSound.systemVolumeFadeToMute(seconds: 5.0, blocking: false)
Check it out: GitHub NSSound_SystemVolumeExtension
Upvotes: 8
Reputation: 4702
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).
Upvotes: 6