SP GlassKit1.1.0

GlassAudioPlayer

Audio transport with play, pause, seek, times and interruption handling; never starts by itself.

  • Custom glass
  • iOS 26 · iPadOS 26 · macOS 26
  • Version 1.1.0
  • glass-audio-player

Draws its own Liquid Glass surface, with an opaque form when Reduce Transparency is on.

GlassAudioPlayer running in SP Glass Studio.Capture
Capture of the component running in SP Glass Studio on the iOS 26.5 simulator. Liquid Glass is rendered by the system; this is an image, not a live preview.

Installation

spglass add glass-audio-player

Copies the sources and their dependencies into your project. Set up the CLI.

Swift
.package(url: "https://git.spboucher.ai/sp-glasskit.git", from: "1.1.0")

import SPGlassKit
Identifierglass-audio-player
ModuleSPGlassMedia
Kindcustom-glass — a transport drawn on one untinted glassSurface; the seek track is drawn by GlassKit and is a system Slider for assistive technologies
PlatformsiOS 26, iPadOS 26, macOS 26
FrameworksAVFoundation (only for GlassAVAudioPlayback)
ExampleExamples/Gallery/Media/GlassAudioPlayerExample.swift

Minimal usage

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

GlassAudioPlayer(playback: playback)

Variants

VariantUse
GlassAudioPlayer(playback:)Artwork tile, seek track, elapsed and remaining times, skip back, play or pause, skip forward.
title: / subtitle:Adds a header with the name of what is playing.
artwork:A cover in the artwork tile, cropped to a square. Without one the tile shows a waveform symbol on a tinted fill.
waveform:A GlassWaveformData: the amplitude bars replace the seek rail and are scrubbed the same way (expanded style).
style: .compactOne row (artwork, titles, play, skip forward) over a thin seek track, for a bar docked above a tab bar.
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 GlassAudioPlaybackThe view is generic over the engine: GlassAVAudioPlayback in production, a fake in previews and tests.

Parameters

GlassAudioPlayer

ParameterTypeDefaultMeaning
playbacksome GlassAudioPlayback—Engine displayed and driven by the view.
titleString?nilName of the audio, shown verbatim.
subtitleString?nilSecondary line.
skipIntervalTimeInterval15Seconds skipped by the skip buttons (at least 1).
artworkImage?nilCover shown in the artwork tile.
waveformGlassWaveformData?nilAmplitudes used as the seek track.
styleGlassAudioPlayerStyle.expanded.expanded or .compact.

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

GlassAVAudioPlayback

MemberMeaning
init(player:updateInterval:)Wraps an AVPlayer owned by the host.
init(url:updateInterval:)Creates the player for a URL. Nothing plays until play().
playerThe AVPlayer, for example to bind its volume.
resumesAfterInterruptionWhether 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 seek track (or the waveform) is dragged the times preview the target; seek(to:) is called once, on release.
  • Unknown duration (duration == 0): the seek track 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.

Example

Complete example (compiled with the package)
import SwiftUI
import SPGlassKit

/// A silent playback engine for the example: it only moves a clock, no audio is produced.
@MainActor
@Observable
final class SampleAudioPlayback: GlassAudioPlayback {
    private(set) var isPlaying = false
    private(set) var currentTime: TimeInterval = 42
    let duration: TimeInterval
    let isAvailable: Bool

    init(duration: TimeInterval = 215, isAvailable: Bool = true) {
        self.duration = duration
        self.isAvailable = isAvailable
    }

    func play() {
        guard isAvailable else { return }
        if currentTime >= duration { currentTime = 0 }
        isPlaying = true
    }

    func pause() { isPlaying = false }

    func seek(to time: TimeInterval) {
        currentTime = GlassPlaybackMath.clampedSeek(time, duration: duration)
    }

    /// Advances the simulated clock; called by the example while "playing".
    func advance(by seconds: TimeInterval) {
        currentTime = GlassPlaybackMath.clampedSeek(currentTime + seconds, duration: duration)
        if currentTime >= duration { isPlaying = false }
    }
}

public struct GlassAudioPlayerExample: GlassExample {
    public static let componentID = "glass-audio-player"
    @State private var playback = SampleAudioPlayback()
    @State private var unavailable = SampleAudioPlayback(duration: 0, isAvailable: false)

    /// Amplitudes of a fictitious recording, aggregated once.
    private static let waveform = GlassWaveformData(samples: GlassWaveformExample.syntheticSamples(count: 9_600), maxBuckets: 64)

    public init() {}

    public var body: some View {
        VStack(spacing: 16) {
            GlassAudioPlayer(playback: playback, title: "Harbour Lines", subtitle: "Sample data · simulated clock, no audio", artwork: SampleArtwork.image(1))
            GlassAudioPlayer(playback: playback, title: "Harbour Lines", subtitle: "With a waveform as the seek track", skipInterval: 30, waveform: Self.waveform)
            GlassAudioPlayer(playback: playback, title: "Harbour Lines", subtitle: "Compact", artwork: SampleArtwork.image(1), style: .compact)
            GlassAudioPlayer(playback: unavailable, title: "Missing recording", skipInterval: 30)
        }
        .padding()
        .task(id: playback.isPlaying) {
            // The fake engine has no audio clock: tick it while it is "playing".
            while playback.isPlaying, !Task.isCancelled {
                try? await Task.sleep(for: .milliseconds(500))
                if !Task.isCancelled { playback.advance(by: 0.5) }
            }
        }
    }
}

#Preview("GlassAudioPlayer") { GlassAudioPlayerExample().glassTheme(.ocean) }

Registry entry

Identifierglass-audio-player
Version1.1.0
Depends onglass-foundation, glass-media-support, glass-waveform
FrameworksSwiftUI, AVFoundation
Public symbolsGlassAudioPlayerStyle, GlassAudioPlayer, GlassAudioPlayerSymbols
LicenceMIT
Manifestmanifest.json

Files