Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP GlassKit7// File : Examples/Gallery/Media/GlassAudioPlayerExample.swift8// Module : Examples9// Repository : https://git.spboucher.ai/sp-glasskit10// Project Website : https://glass.spboucher.ai11// Created : 2026-12-0212// Last Modified : 2026-12-0213//14// Author : Simon-Pierre Boucher15// Position : Professor of Finance16// Institution : Université du Québec en Outaouais (UQO)17// Organization : SP Boucher18// Website : https://spboucher.ai19// Git : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development : dev@spboucher.ai23// Support : support@spboucher.ai24// Security/Admin : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License : MIT33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import SwiftUI42import SPGlassKit4344/// A silent playback engine for the example: it only moves a clock, no audio is produced.45@MainActor46@Observable47final class SampleAudioPlayback: GlassAudioPlayback {48 private(set) var isPlaying = false49 private(set) var currentTime: TimeInterval = 4250 let duration: TimeInterval51 let isAvailable: Bool5253 init(duration: TimeInterval = 215, isAvailable: Bool = true) {54 self.duration = duration55 self.isAvailable = isAvailable56 }5758 func play() {59 guard isAvailable else { return }60 if currentTime >= duration { currentTime = 0 }61 isPlaying = true62 }6364 func pause() { isPlaying = false }6566 func seek(to time: TimeInterval) {67 currentTime = GlassPlaybackMath.clampedSeek(time, duration: duration)68 }6970 /// Advances the simulated clock; called by the example while "playing".71 func advance(by seconds: TimeInterval) {72 currentTime = GlassPlaybackMath.clampedSeek(currentTime + seconds, duration: duration)73 if currentTime >= duration { isPlaying = false }74 }75}7677public struct GlassAudioPlayerExample: GlassExample {78 public static let componentID = "glass-audio-player"79 @State private var playback = SampleAudioPlayback()80 @State private var unavailable = SampleAudioPlayback(duration: 0, isAvailable: false)8182 public init() {}8384 public var body: some View {85 VStack(spacing: 16) {86 GlassAudioPlayer(playback: playback, title: "Morning Tide", subtitle: "Sample data · simulated clock, no audio")87 GlassAudioPlayer(playback: unavailable, title: "Missing recording", skipInterval: 30)88 }89 .padding()90 .task(id: playback.isPlaying) {91 // The fake engine has no audio clock: tick it while it is "playing".92 while playback.isPlaying, !Task.isCancelled {93 try? await Task.sleep(for: .milliseconds(500))94 if !Task.isCancelled { playback.advance(by: 0.5) }95 }96 }97 }98}99100#Preview("GlassAudioPlayer") { GlassAudioPlayerExample().glassTheme(.ocean) }101