Repository Public MIT
1# GlassAudioPlayer23| | |4|---|---|5| Identifier | `glass-audio-player` |6| Module | `SPGlassMedia` |7| Kind | `custom-glass` — a transport drawn on one untinted `glassSurface`; the seek track is drawn by GlassKit and is a system `Slider` for assistive technologies |8| Platforms | iOS 26, iPadOS 26, macOS 26 |9| Frameworks | AVFoundation (only for `GlassAVAudioPlayback`) |10| Example | `Examples/Gallery/Media/GlassAudioPlayerExample.swift` |1112## Minimal usage1314```swift15@State private var playback = GlassAVAudioPlayback(url: episodeURL)1617GlassAudioPlayer(playback: playback)18```1920## Variants2122| Variant | Use |23|---|---|24| `GlassAudioPlayer(playback:)` | Artwork tile, seek track, elapsed and remaining times, skip back, play or pause, skip forward. |25| `title:` / `subtitle:` | Adds a header with the name of what is playing. |26| `artwork:` | A cover in the artwork tile, cropped to a square. Without one the tile shows a waveform symbol on a tinted fill. |27| `waveform:` | A `GlassWaveformData`: the amplitude bars replace the seek rail and are scrubbed the same way (expanded style). |28| `style: .compact` | One row (artwork, titles, play, skip forward) over a thin seek track, for a bar docked above a tab bar. |29| `skipInterval:` | Seconds skipped by the two skip buttons (default 15). Numbered symbols exist for 5, 10, 15, 30, 45, 60, 75 and 90; other values use the plain arrows. |30| Any `GlassAudioPlayback` | The view is generic over the engine: `GlassAVAudioPlayback` in production, a fake in previews and tests. |3132## Parameters3334`GlassAudioPlayer`3536| Parameter | Type | Default | Meaning |37|---|---|---|---|38| `playback` | `some GlassAudioPlayback` | — | Engine displayed and driven by the view. |39| `title` | `String?` | `nil` | Name of the audio, shown verbatim. |40| `subtitle` | `String?` | `nil` | Secondary line. |41| `skipInterval` | `TimeInterval` | `15` | Seconds skipped by the skip buttons (at least 1). |42| `artwork` | `Image?` | `nil` | Cover shown in the artwork tile. |43| `waveform` | `GlassWaveformData?` | `nil` | Amplitudes used as the seek track. |44| `style` | `GlassAudioPlayerStyle` | `.expanded` | `.expanded` or `.compact`. |4546`GlassAudioPlayback` (protocol, `@MainActor`, `Observable`): `isPlaying`, `currentTime`, `duration` (0 while unknown), `isAvailable`, `play()`, `pause()`, `seek(to:)`.4748`GlassAVAudioPlayback`4950| Member | Meaning |51|---|---|52| `init(player:updateInterval:)` | Wraps an `AVPlayer` owned by the host. |53| `init(url:updateInterval:)` | Creates the player for a URL. Nothing plays until `play()`. |54| `player` | The `AVPlayer`, for example to bind its `volume`. |55| `resumesAfterInterruption` | Whether playback resumes after a resumable interruption (default `true`). |56| `replaceItem(with:)` | Changes the URL; playback stays paused. |57| `configureAudioSessionForPlayback()` | iOS only, static, never called by GlassKit: sets the `.playback` category. |5859## States6061- **Paused / playing**: the centre button shows play or pause and its label follows.62- **Scrubbing**: while the seek track (or the waveform) is dragged the times preview the target; `seek(to:)` is called once, on release.63- **Unknown duration** (`duration == 0`): the seek track is disabled, the times read 0:00.64- **Unavailable** (`isAvailable == false`): the transport is replaced by "Media unavailable" with a symbol.65- **Ended**: `GlassAVAudioPlayback` stops at the end; the next `play()` restarts from the beginning.6667## Advanced example6869```swift70struct EpisodeView: View {71 @State private var playback: GlassAVAudioPlayback72 @State private var volume = 1.07374 init(url: URL) {75 _playback = State(initialValue: GlassAVAudioPlayback(url: url))76 }7778 var body: some View {79 VStack {80 GlassAudioPlayer(playback: playback, title: "Episode 12", subtitle: "The Glass Hour", skipInterval: 30)81 GlassVolumeControl(volume: $volume)82 }83 .onChange(of: volume) { _, level in playback.player.volume = Float(level) }84 .task {85 #if os(iOS)86 try? GlassAVAudioPlayback.configureAudioSessionForPlayback() // the host's decision87 #endif88 }89 }90}91```9293## Accessibility9495- Every button has a label ("Play", "Pause", "Skip back", "Skip forward") and a tooltip on macOS; hit targets are at least 44 points on iOS.96- The slider is labelled "Playback position" and its value is spoken as "1:05 of 3:20"; it is adjustable with the standard VoiceOver gestures.97- Elapsed and remaining times are labelled; they use monospaced digits and Dynamic Type.98- Space toggles play and pause with a hardware keyboard.99- With Reduce Transparency the surface becomes opaque (handled by `glassSurface`).100101## Limits102103- Times are formatted with `Duration.TimeFormatStyle` (`m:ss`, `h:mm:ss` from one hour).104- `GlassAVAudioPlayback` observes the player with a periodic time observer (removed on deinit), key-value observation of `timeControlStatus` and item notifications. It does not manage a queue, Now Playing information or remote commands: those belong to the host.105- The audio session category is never changed by GlassKit. Interruptions are handled on iOS only (there is no `AVAudioSession` on macOS).106- Real playback, interruptions and end-of-item behaviour need a device or simulator with audio; the test suite covers the pure logic (clamping, formatting, interruption policy) and an item-less player only.107