SPB Git

Repository Public MIT

glass-volume-control.md 2.8 KB 72 lines markdown

1# GlassVolumeControl23| | |4|---|---|5| Identifier | `glass-volume-control` |6| Module | `SPGlassMedia` |7| Kind | `native-wrapper` — a plain system button and the system `Slider` |8| Platforms | iOS 26, iPadOS 26, macOS 26 |9| Example | `Examples/Gallery/Media/GlassVolumeControlExample.swift` |1011## Minimal usage1213```swift14@State private var volume = 0.81516GlassVolumeControl(volume: $volume)17```1819## Variants2021| Variant | Use |22|---|---|23| Default | Mute button followed by the slider. |24| `unmuteLevel:` | Level restored by the first unmute when the control starts at 0 (default 0.5). |25| `showsValue: true` | The level as a whole percentage after the slider, in tabular figures. |2627## Parameters2829| Parameter | Type | Default | Meaning |30|---|---|---|---|31| `volume` | `Binding<Double>` | — | Level of the application's own playback, 0…1. Out-of-range values are clamped. |32| `unmuteLevel` | `Double` | `0.5` | Level used by an unmute when no audible level was seen yet. |33| `showsValue` | `Bool` | `false` | Shows the percentage after the slider (hidden from assistive technologies, which read the slider's value). |3435The host applies the level to its player, for example `player.volume = Float(volume)`.3637## States3839- **Audible**: the symbol shows one, two or three waves (below 34 %, below 67 %, above).40- **Muted** (level 0): slashed speaker; the button's label becomes "Unmute". The level before muting is remembered and restored by unmuting; dragging the slider to 0 keeps the last audible level too.41- **Disabled**: the standard `.disabled(_:)` modifier.4243The reducer is `GlassVolumeState` (`togglingMute()`, `settingLevel(_:)`), a value type that can be used without the view.4445## Advanced example4647```swift48struct PlayerVolume: View {49    let player: AVPlayer50    @AppStorage("playbackVolume") private var volume = 0.85152    var body: some View {53        GlassVolumeControl(volume: $volume)54            .frame(maxWidth: 240)55            .onChange(of: volume, initial: true) { _, level in56                player.volume = Float(level)57            }58    }59}60```6162## Accessibility6364- The button is labelled "Mute" or "Unmute" (also its tooltip on macOS) and has a 44-point target on iOS.65- The slider is labelled "Volume"; its value is spoken as a localised percentage, or "Muted" at 0. It is adjustable with VoiceOver.66- The level is conveyed by the symbol's shape and the spoken value, not by colour.6768## Limits6970- The control never changes the system volume. On iOS the system volume is only reachable through `MPVolumeView`, which the host places itself; there is no public API to set it programmatically. On macOS the system volume belongs to the menu bar and the keyboard.71- The remembered level lives in the view's state: it is lost when the view is destroyed. Persist the level in the host if needed.72
Files main
Menu

Theme