SPB Git

Repository Public MIT

glass-audio-player.md 4.6 KB

# GlassAudioPlayer

Identifier glass-audio-player
Module SPGlassMedia
Kind custom-glass — a transport drawn on one glassSurface; the slider is the system Slider
Platforms iOS 26, iPadOS 26, macOS 26
Frameworks AVFoundation (only for GlassAVAudioPlayback)
Example Examples/Gallery/Media/GlassAudioPlayerExample.swift

# Minimal usage

swift
@State private var playback = GlassAVAudioPlayback(url: episodeURL)

GlassAudioPlayer(playback: playback)

# Variants

Variant Use
GlassAudioPlayer(playback:) Transport only: seek slider, elapsed and remaining times, skip back, play or pause, skip forward.
title: / subtitle: Adds a header with the name of what is playing.
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.
Any GlassAudioPlayback The view is generic over the engine: GlassAVAudioPlayback in production, a fake in previews and tests.

# Parameters

GlassAudioPlayer

Parameter Type Default Meaning
playback some GlassAudioPlayback — Engine displayed and driven by the view.
title String? nil Name of the audio, shown verbatim.
subtitle String? nil Secondary line.
skipInterval TimeInterval 15 Seconds skipped by the skip buttons (at least 1).

GlassAudioPlayback (protocol, @MainActor, Observable): isPlaying, currentTime, duration (0 while unknown), isAvailable, play(), pause(), seek(to:).

GlassAVAudioPlayback

Member Meaning
init(player:updateInterval:) Wraps an AVPlayer owned by the host.
init(url:updateInterval:) Creates the player for a URL. Nothing plays until play().
player The AVPlayer, for example to bind its volume.
resumesAfterInterruption Whether playback resumes after a resumable interruption (default true).
replaceItem(with:) Changes the URL; playback stays paused.
configureAudioSessionForPlayback() iOS only, static, never called by GlassKit: sets the .playback category.

# States

  • Paused / playing: the centre button shows play or pause and its label follows.
  • Scrubbing: while the slider is dragged the times preview the target; seek(to:) is called once, on release.
  • Unknown duration (duration == 0): the slider is disabled, the times read 0:00.
  • Unavailable (isAvailable == false): the transport is replaced by "Media unavailable" with a symbol.
  • Ended: GlassAVAudioPlayback stops at the end; the next play() restarts from the beginning.

# Advanced example

swift
struct EpisodeView: View {
    @State private var playback: GlassAVAudioPlayback
    @State private var volume = 1.0

    init(url: URL) {
        _playback = State(initialValue: GlassAVAudioPlayback(url: url))
    }

    var body: some View {
        VStack {
            GlassAudioPlayer(playback: playback, title: "Episode 12", subtitle: "The Glass Hour", skipInterval: 30)
            GlassVolumeControl(volume: $volume)
        }
        .onChange(of: volume) { _, level in playback.player.volume = Float(level) }
        .task {
            #if os(iOS)
            try? GlassAVAudioPlayback.configureAudioSessionForPlayback()   // the host's decision
            #endif
        }
    }
}

# Accessibility

  • 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.
  • 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.
  • Elapsed and remaining times are labelled; they use monospaced digits and Dynamic Type.
  • Space toggles play and pause with a hardware keyboard.
  • With Reduce Transparency the surface becomes opaque (handled by glassSurface).

# Limits

  • Times are formatted with Duration.TimeFormatStyle (m:ss, h:mm:ss from one hour).
  • 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.
  • The audio session category is never changed by GlassKit. Interruptions are handled on iOS only (there is no AVAudioSession on macOS).
  • 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.
Files main
Menu

Theme