GlassVolumeControl
Volume of the application's own playback, with mute.
- System control
- iOS 26 · iPadOS 26 · macOS 26
- Version 1.1.0
glass-volume-control
Drawn by the system. GlassKit configures it and leaves the glass to the system.
CaptureInstallation
spglass add glass-volume-controlCopies the sources and their dependencies into your project. Set up the CLI.
.package(url: "https://git.spboucher.ai/sp-glasskit.git", from: "1.1.0")
import SPGlassKit| 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
@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
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.
Example
import SwiftUI
import SPGlassKit
public struct GlassVolumeControlExample: GlassExample {
public static let componentID = "glass-volume-control"
@State private var musicVolume = 0.65
@State private var effectsVolume = 0.0
public init() {}
public var body: some View {
Form {
Section("Music") {
GlassVolumeControl(volume: $musicVolume, showsValue: true)
LabeledContent("Level applied to the player", value: musicVolume, format: .percent.precision(.fractionLength(0)))
}
Section("Effects (starts muted)") {
GlassVolumeControl(volume: $effectsVolume, unmuteLevel: 0.4)
}
Section("Disabled") {
GlassVolumeControl(volume: .constant(0.3))
.disabled(true)
}
}
.formStyle(.grouped)
}
}
#Preview("GlassVolumeControl") { GlassVolumeControlExample().glassTheme(.emerald) }
Registry entry
| Identifier | glass-volume-control |
|---|---|
| Version | 1.1.0 |
| Depends on | glass-foundation |
| Frameworks | SwiftUI |
| Public symbols | GlassVolumeControl, GlassVolumeState |
| Licence | MIT |
| Manifest | manifest.json |
Files
Media/Media/GlassVolumeControl.swift7443 bytes · sha256 ae6f9381ae90…