Repository Public MIT
1# GlassAudioPlayer23| | |4|---|---|5| Identifier | `glass-audio-player` |6| Module | `SPGlassMedia` |7| Kind | `custom-glass` — a transport drawn on one `glassSurface`; the slider is the system `Slider` |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:)` | Transport only: seek slider, 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| `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. |27| Any `GlassAudioPlayback` | The view is generic over the engine: `GlassAVAudioPlayback` in production, a fake in previews and tests. |2829## Parameters3031`GlassAudioPlayer`3233| Parameter | Type | Default | Meaning |34|---|---|---|---|35| `playback` | `some GlassAudioPlayback` | — | Engine displayed and driven by the view. |36| `title` | `String?` | `nil` | Name of the audio, shown verbatim. |37| `subtitle` | `String?` | `nil` | Secondary line. |38| `skipInterval` | `TimeInterval` | `15` | Seconds skipped by the skip buttons (at least 1). |3940`GlassAudioPlayback` (protocol, `@MainActor`, `Observable`): `isPlaying`, `currentTime`, `duration` (0 while unknown), `isAvailable`, `play()`, `pause()`, `seek(to:)`.4142`GlassAVAudioPlayback`4344| Member | Meaning |45|---|---|46| `init(player:updateInterval:)` | Wraps an `AVPlayer` owned by the host. |47| `init(url:updateInterval:)` | Creates the player for a URL. Nothing plays until `play()`. |48| `player` | The `AVPlayer`, for example to bind its `volume`. |49| `resumesAfterInterruption` | Whether playback resumes after a resumable interruption (default `true`). |50| `replaceItem(with:)` | Changes the URL; playback stays paused. |51| `configureAudioSessionForPlayback()` | iOS only, static, never called by GlassKit: sets the `.playback` category. |5253## States5455- **Paused / playing**: the centre button shows play or pause and its label follows.56- **Scrubbing**: while the slider is dragged the times preview the target; `seek(to:)` is called once, on release.57- **Unknown duration** (`duration == 0`): the slider is disabled, the times read 0:00.58- **Unavailable** (`isAvailable == false`): the transport is replaced by "Media unavailable" with a symbol.59- **Ended**: `GlassAVAudioPlayback` stops at the end; the next `play()` restarts from the beginning.6061## Advanced example6263```swift64struct EpisodeView: View {65 @State private var playback: GlassAVAudioPlayback66 @State private var volume = 1.06768 init(url: URL) {69 _playback = State(initialValue: GlassAVAudioPlayback(url: url))70 }7172 var body: some View {73 VStack {74 GlassAudioPlayer(playback: playback, title: "Episode 12", subtitle: "The Glass Hour", skipInterval: 30)75 GlassVolumeControl(volume: $volume)76 }77 .onChange(of: volume) { _, level in playback.player.volume = Float(level) }78 .task {79 #if os(iOS)80 try? GlassAVAudioPlayback.configureAudioSessionForPlayback() // the host's decision81 #endif82 }83 }84}85```8687## Accessibility8889- 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.90- 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.91- Elapsed and remaining times are labelled; they use monospaced digits and Dynamic Type.92- Space toggles play and pause with a hardware keyboard.93- With Reduce Transparency the surface becomes opaque (handled by `glassSurface`).9495## Limits9697- Times are formatted with `Duration.TimeFormatStyle` (`m:ss`, `h:mm:ss` from one hour).98- `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.99- The audio session category is never changed by GlassKit. Interruptions are handled on iOS only (there is no `AVAudioSession` on macOS).100- 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.101