From 6fe943f15fa13aa4fb03bb49b167e35882d07cec Mon Sep 17 00:00:00 2001 From: Laurent Date: Tue, 14 Mar 2023 23:45:14 +0100 Subject: [PATCH] cleanup audio session activation --- LeCountdown.xcodeproj/project.pbxproj | 2 -- LeCountdown/Conductor.swift | 13 +++++++++++++ LeCountdown/Sound/SoundPlayer.swift | 2 +- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/LeCountdown.xcodeproj/project.pbxproj b/LeCountdown.xcodeproj/project.pbxproj index d60799a..f8a5b09 100644 --- a/LeCountdown.xcodeproj/project.pbxproj +++ b/LeCountdown.xcodeproj/project.pbxproj @@ -178,7 +178,6 @@ C4F8B1532987FE6F005C86A5 /* LaunchWidgetLiveActivity.swift in Sources */ = {isa = PBXBuildFile; fileRef = C438C7D72981216200BF3EF9 /* LaunchWidgetLiveActivity.swift */; }; C4F8B15729891271005C86A5 /* Conductor.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4F8B15629891271005C86A5 /* Conductor.swift */; }; C4F8B15929891528005C86A5 /* forest_stream.mp3 in Resources */ = {isa = PBXBuildFile; fileRef = C4F8B15829891528005C86A5 /* forest_stream.mp3 */; }; - C4F8B15B29892D40005C86A5 /* SoundPlayer.swift in Sources */ = {isa = PBXBuildFile; fileRef = C445FA8E2987B83B0054D761 /* SoundPlayer.swift */; }; C4F8B15F298961A7005C86A5 /* ReorderableForEach.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4F8B15E298961A7005C86A5 /* ReorderableForEach.swift */; }; C4F8B162298A9A1F005C86A5 /* NewAlarmView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4F8B161298A9A1F005C86A5 /* NewAlarmView.swift */; }; C4F8B164298A9A92005C86A5 /* AlarmFormView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4F8B163298A9A92005C86A5 /* AlarmFormView.swift */; }; @@ -1183,7 +1182,6 @@ C4F8B194298AC288005C86A5 /* Record+CoreDataProperties.swift in Sources */, C438C8152982BD9000BF3EF9 /* IntentDataProvider.swift in Sources */, C473C31929A926F50056B38A /* LaunchWidget.intentdefinition in Sources */, - C4F8B15B29892D40005C86A5 /* SoundPlayer.swift in Sources */, C4BA2B5C299FFAB000CB4FBA /* Logger.swift in Sources */, C473C2FA29A8DC1E0056B38A /* LaunchWidgetAttributes.swift in Sources */, C438C7E82981255D00BF3EF9 /* TimeInterval+Extensions.swift in Sources */, diff --git a/LeCountdown/Conductor.swift b/LeCountdown/Conductor.swift index 852c7dd..a3e9d2e 100644 --- a/LeCountdown/Conductor.swift +++ b/LeCountdown/Conductor.swift @@ -11,6 +11,7 @@ import SwiftUI import Intents import AudioToolbox import ActivityKit +import AVFoundation enum BGTaskIdentifier : String { case refresh = "com.staxriver.lecountdown.refresh" @@ -308,6 +309,18 @@ class Conductor: ObservableObject { soundPlayer.stop() self._delayedSoundPlayers.removeValue(forKey: id) } + + self.deactivateAudioSessionIfPossible() + } + + func deactivateAudioSessionIfPossible() { + if self._delayedSoundPlayers.isEmpty { + do { + try AVAudioSession.sharedInstance().setActive(false) + } catch { + Logger.error(error) + } + } } func stopMainPlayersIfPossible() { diff --git a/LeCountdown/Sound/SoundPlayer.swift b/LeCountdown/Sound/SoundPlayer.swift index 21a8fae..ac8b868 100644 --- a/LeCountdown/Sound/SoundPlayer.swift +++ b/LeCountdown/Sound/SoundPlayer.swift @@ -72,7 +72,7 @@ enum SoundPlayerError : Error { // MARK: - Delegate func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) { - + Conductor.maestro.deactivateAudioSessionIfPossible() } }