import AVFoundation import SwiftUI @main struct mlbTVOSApp: App { @State private var viewModel = GamesViewModel() @Environment(\.scenePhase) private var scenePhase init() { configureAudioSession() } var body: some Scene { WindowGroup { ContentView() .environment(viewModel) .onChange(of: scenePhase) { _, newPhase in if newPhase == .active { Task { await viewModel.refreshIfDayChanged() } } } } } private func configureAudioSession() { // Start with .ambient so we don't interrupt other audio on launch // Switch to .playback when user starts a stream do { try AVAudioSession.sharedInstance().setCategory(.ambient) } catch { print("Failed to set audio session: \(error)") } NotificationCenter.default.addObserver( forName: AVAudioSession.interruptionNotification, object: AVAudioSession.sharedInstance(), queue: .main ) { notification in guard let info = notification.userInfo, let typeValue = info[AVAudioSessionInterruptionTypeKey] as? UInt, let type = AVAudioSession.InterruptionType(rawValue: typeValue) else { return } if type == .ended { let options = (info[AVAudioSessionInterruptionOptionKey] as? UInt) .flatMap(AVAudioSession.InterruptionOptions.init) ?? [] if options.contains(.shouldResume) { try? AVAudioSession.sharedInstance().setActive(true) } } } } }