SoundOutputManager
public final class SoundOutputManager
Mute, unmute and change the volume of the system default output device.
Overview
You can interact with this class in two ways:
- you can interact with its properties, meaning that all changes will be applied immediately and errors will be hidden.
- you can call its methods and handle errors manually.
-
Get the system default output device.
You can use this value to interact with the device directly via other system calls.
Throws
Errors.operationFailed
if the system fails to return the default output device.Declaration
Swift
public func retrieveDefaultOutputDevice() throws -> AudioDeviceID?
Return Value
the default device ID or
nil
if none is set. -
Get the volume of the system default output device.
Throws
Errors.noDevice
if the system doesn’t have a default output device;Errors.unsupportedProperty
if the current device doesn’t have a volume property;Errors.operationFailed
if the system is unable to read the property value.Declaration
Swift
public func readVolume() throws -> Float
Return Value
The current volume in a range between 0 and 1.
-
Set the volume of the system default output device.
Throws
Erors.noDevice
if the system doesn’t have a default output device;Errors.unsupportedProperty
orErrors.immutableProperty
if the output device doesn’t support setting or doesn’t currently allow changes to its volume;Errors.operationFailed
if the system is unable to apply the volume change.Declaration
Swift
public func setVolume(_ newValue: Float) throws
Parameters
newValue
The volume to set in a range between 0 and 1.
-
Get whether the system default output device is currently muted or not.
Throws
Errors.noDevice
if the system doesn’t have a default output device;Errors.unsupportedProperty
if the current device doesn’t have a mute property;Errors.operationFailed
if the system is unable to read the property value.Declaration
Swift
public func readMute() throws -> Bool
Return Value
Whether the device is muted or not.
-
Mute or unmute the system default output device.
Throws
Errors.noDevice
if the system doesn’t have a default output device;Errors.unsupportedProperty
orErrors.immutableProperty
if the output device doesn’t support setting or doesn’t currently allow changes to its mute property;Errors.operationFailed
if the system is unable to apply the change.Declaration
Swift
public func mute(_ isMuted: Bool) throws
Parameters
isMuted
Mute or unmute.
-
Increase the volume of the default output device by the given amount.
Errors will be ignored.
The values range between 0 and 1. If the increase results in a value outside of the bounds, it will be normalized to the closest value in the bounds.
Declaration
Swift
func increaseVolume(by value: Float, autoMuteUnmute: Bool = false, muteThreshold: Float = 0.005)
-
Decrease the volume of the default output device by the given amount.
Errors will be ignored.
The values range between 0 and 1. If the decrease results in a value outside of the bounds, it will be normalized to the closest value in the bounds.
Declaration
Swift
func decreaseVolume(by value: Float, autoMuteUnmute: Bool = false, muteThreshold: Float = 0.005)
-
Set the volume of the default output device and, if lower or higher then
muteThreshold
also toggles the mute property.Warning
This function will unmute a muted device, if the volume is greater then
muteThreshold
. Please, make sure that the user is aware of this and always respect the Do Not Disturb modes and other system settings.Declaration
Swift
func setVolume(_ newValue: Float, autoMuteUnmute: Bool, muteThreshold: Float = 0.005)
Parameters
newValue
The volume.
autoMuteUnmute
If
true
, will use themuteThreshold
to determine whether the device should also be muted or unmuted.muteThreshold
Defines the threshold that should cause an automatic mute for all values below it.
-
Get the system default output device.
You can use this value to interact with the device directly via other system calls.
This value will return
nil
if there is currently no device selected in System Preferences > Sound > Output.Declaration
Swift
var defaultOutputDevice: AudioDeviceID? { get }
-
Get or set the volume of the default output device.
Errors will be ignored. If you need to handle errors, use
readVolume
andsetVolume
.The values range between 0 and 1.
Declaration
Swift
var volume: Float { get set }
-
Get or set whether the system default output device is muted or not.
Errors will be ignored. If you need to handle errors, use
readMute
andmute
. Devices that do not support muting will always returnfalse
.Declaration
Swift
var isMuted: Bool { get set }