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.operationFailedif the system fails to return the default output device.Declaration
Swift
public func retrieveDefaultOutputDevice() throws -> AudioDeviceID?Return Value
the default device ID or
nilif none is set. -
Get the volume of the system default output device.
Throws
Errors.noDeviceif the system doesn’t have a default output device;Errors.unsupportedPropertyif the current device doesn’t have a volume property;Errors.operationFailedif the system is unable to read the property value.Declaration
Swift
public func readVolume() throws -> FloatReturn Value
The current volume in a range between 0 and 1.
-
Set the volume of the system default output device.
Throws
Erors.noDeviceif the system doesn’t have a default output device;Errors.unsupportedPropertyorErrors.immutablePropertyif the output device doesn’t support setting or doesn’t currently allow changes to its volume;Errors.operationFailedif the system is unable to apply the volume change.Declaration
Swift
public func setVolume(_ newValue: Float) throwsParameters
newValueThe volume to set in a range between 0 and 1.
-
Get whether the system default output device is currently muted or not.
Throws
Errors.noDeviceif the system doesn’t have a default output device;Errors.unsupportedPropertyif the current device doesn’t have a mute property;Errors.operationFailedif the system is unable to read the property value.Declaration
Swift
public func readMute() throws -> BoolReturn Value
Whether the device is muted or not.
-
Mute or unmute the system default output device.
Throws
Errors.noDeviceif the system doesn’t have a default output device;Errors.unsupportedPropertyorErrors.immutablePropertyif the output device doesn’t support setting or doesn’t currently allow changes to its mute property;Errors.operationFailedif the system is unable to apply the change.Declaration
Swift
public func mute(_ isMuted: Bool) throwsParameters
isMutedMute 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
muteThresholdalso 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
newValueThe volume.
autoMuteUnmuteIf
true, will use themuteThresholdto determine whether the device should also be muted or unmuted.muteThresholdDefines 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
nilif 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
readVolumeandsetVolume.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
readMuteandmute. Devices that do not support muting will always returnfalse.Declaration
Swift
var isMuted: Bool { get set }
SoundOutputManager Class Reference