|
| 1 | +// |
| 2 | +// Volume.swift |
| 3 | +// ControlKit |
| 4 | +// |
| 5 | + |
| 6 | +import MediaPlayer |
| 7 | + |
| 8 | +public extension Control { |
| 9 | + |
| 10 | + /// Control the system volume using the underlying `MPVolumeView`. |
| 11 | + @MainActor |
| 12 | + enum Volume { |
| 13 | + |
| 14 | + /// Subscribe to `volume` via ``VolumeController/volume`` `@Published` property. |
| 15 | + /// |
| 16 | + /// - Important: Updating this property will _unmute_ the system volume. The volume level prior to being muted will |
| 17 | + /// be ignored when setting the volume via this property. |
| 18 | + public static var volume: Float { |
| 19 | + get { |
| 20 | + MPVolumeView.volume |
| 21 | + } |
| 22 | + set { |
| 23 | + if newValue != 0 && isMuted { |
| 24 | + isMuted.toggle() |
| 25 | + } |
| 26 | + MPVolumeView.volume = newValue |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + /// Subscribe to `isMuted` via ``VolumeController/isMuted`` `@Published` property. |
| 31 | + public static var isMuted = false { |
| 32 | + didSet { |
| 33 | + if isMuted { |
| 34 | + Helpers.mutedVolumeLevel = volume |
| 35 | + } |
| 36 | + volume = isMuted ? 0 : Helpers.mutedVolumeLevel |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + /// Increments the system volume, mimicking when a user taps the volume rocker on their phone. |
| 41 | + /// |
| 42 | + /// - Parameter amount: clamped between 0 and 1.0 using ``BetweenZeroAndOneInclusive``. |
| 43 | + /// |
| 44 | + /// - Important: Calling this function will _unmute_ the system volume. The increment amount is |
| 45 | + /// applied to the volume level prior to it being muted. |
| 46 | + public static func increase( |
| 47 | + @BetweenZeroAndOneInclusive _ amount: Float = Helpers.defaultVolumeStep |
| 48 | + ) { |
| 49 | + volume += amount |
| 50 | + } |
| 51 | + |
| 52 | + /// Decrements the system volume, mimicking when a user taps the volume rocker on their phone. |
| 53 | + /// |
| 54 | + /// - Parameter amount: clamped between 0 and 1.0 using ``BetweenZeroAndOneInclusive``. |
| 55 | + /// |
| 56 | + /// - Important: Calling this function will _unmute_ the system volume. The decrement amount is |
| 57 | + /// applied to the volume level prior to it being muted. |
| 58 | + public static func decrease( |
| 59 | + @BetweenZeroAndOneInclusive _ amount: Float = Helpers.defaultVolumeStep |
| 60 | + ) { |
| 61 | + volume -= amount |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +extension Control.Volume { |
| 67 | + |
| 68 | + public enum Helpers { |
| 69 | + |
| 70 | + /// Refers to the amount (on scale from 0 to 1) the volume is incremented/decremented when the volume rocker is pressed on the phone. |
| 71 | + public static let defaultVolumeStep: Float = 1 / maxVolumeButtonPresses |
| 72 | + |
| 73 | + /// Refers to the volume level prior to it being muted. |
| 74 | + static var mutedVolumeLevel: Float = 0 |
| 75 | + |
| 76 | + /// Refers to the number of (volume rocker) button presses it takes for the phone's volume to go from 0 to max. |
| 77 | + private static let maxVolumeButtonPresses: Float = 16 |
| 78 | + } |
| 79 | +} |
0 commit comments