cleanup audio session activation

main
Laurent 3 years ago
parent 762d7b62b3
commit 6fe943f15f
  1. 2
      LeCountdown.xcodeproj/project.pbxproj
  2. 13
      LeCountdown/Conductor.swift
  3. 2
      LeCountdown/Sound/SoundPlayer.swift

@ -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 */,

@ -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() {

@ -72,7 +72,7 @@ enum SoundPlayerError : Error {
// MARK: - Delegate
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
Conductor.maestro.deactivateAudioSessionIfPossible()
}
}

Loading…
Cancel
Save