SPB Git

Repository Public MIT

glass-volume-control.md 2.8 KB

# GlassVolumeControl

Identifier glass-volume-control
Module SPGlassMedia
Kind native-wrapper — a plain system button and the system Slider
Platforms iOS 26, iPadOS 26, macOS 26
Example Examples/Gallery/Media/GlassVolumeControlExample.swift

# Minimal usage

swift
@State private var volume = 0.8

GlassVolumeControl(volume: $volume)

# Variants

Variant Use
Default Mute button followed by the slider.
unmuteLevel: Level restored by the first unmute when the control starts at 0 (default 0.5).
showsValue: true The level as a whole percentage after the slider, in tabular figures.

# Parameters

Parameter Type Default Meaning
volume Binding<Double> — Level of the application's own playback, 0…1. Out-of-range values are clamped.
unmuteLevel Double 0.5 Level used by an unmute when no audible level was seen yet.
showsValue Bool false Shows the percentage after the slider (hidden from assistive technologies, which read the slider's value).

The host applies the level to its player, for example player.volume = Float(volume).

# States

  • Audible: the symbol shows one, two or three waves (below 34 %, below 67 %, above).
  • 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.
  • Disabled: the standard .disabled(_:) modifier.

The reducer is GlassVolumeState (togglingMute(), settingLevel(_:)), a value type that can be used without the view.

# Advanced example

swift
struct PlayerVolume: View {
    let player: AVPlayer
    @AppStorage("playbackVolume") private var volume = 0.8

    var body: some View {
        GlassVolumeControl(volume: $volume)
            .frame(maxWidth: 240)
            .onChange(of: volume, initial: true) { _, level in
                player.volume = Float(level)
            }
    }
}

# Accessibility

  • The button is labelled "Mute" or "Unmute" (also its tooltip on macOS) and has a 44-point target on iOS.
  • The slider is labelled "Volume"; its value is spoken as a localised percentage, or "Muted" at 0. It is adjustable with VoiceOver.
  • The level is conveyed by the symbol's shape and the spoken value, not by colour.

# Limits

  • 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.
  • 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.
Files main
Menu

Theme