Sound improvements

main
Laurent 3 years ago
parent 19858a6a3c
commit de847a739e
  1. 7
      LeCountdown.xcodeproj/xcshareddata/xcschemes/LeCountdown.xcscheme
  2. 2
      LeCountdown/AppDelegate.swift
  3. 20
      LeCountdown/Conductor.swift
  4. 15
      LeCountdown/LeCountdownApp.swift
  5. 6
      LeCountdown/Model/NSManagedContext+Extensions.swift
  6. 2
      LeCountdown/Sound/DelaySoundPlayer.swift
  7. 1
      LeCountdown/Sound/SoundPlayer.swift

@ -64,6 +64,13 @@
isEnabled = "YES">
</CommandLineArgument>
</CommandLineArguments>
<AdditionalOptions>
<AdditionalOption
key = "NSZombieEnabled"
value = "YES"
isEnabled = "YES">
</AdditionalOption>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"

@ -41,10 +41,12 @@ class AppDelegate : NSObject, UIApplicationDelegate {
}
func applicationWillTerminate(_ application: UIApplication) {
Logger.log("applicationWillTerminate")
FileLogger.log("applicationWillTerminate")
}
func applicationDidReceiveMemoryWarning(_ application: UIApplication) {
Logger.log("applicationDidReceiveMemoryWarning")
FileLogger.log("applicationDidReceiveMemoryWarning")
}

@ -136,25 +136,26 @@ class Conductor: ObservableObject {
DispatchQueue.main.async {
let countdownId = countdown.stringId
self._cleanupCurrentTimerIfNecessary(countdownId)
do {
let start = Date()
let end = start.addingTimeInterval(countdown.duration)
FileLogger.log("schedule countdown \(countdown.stringId) at \(end)")
self.removeLiveTimer(id: countdown.stringId)
FileLogger.log("schedule countdown \(countdownId) at \(end)")
let sound = countdown.someSound
let soundPlayer = try DelaySoundPlayer(timerID: countdown.stringId, sound: sound)
let soundPlayer = try DelaySoundPlayer(timerID: countdownId, sound: sound)
FileLogger.log("a) self._delayedSoundPlayers count = \(self._delayedSoundPlayers.count)")
self._delayedSoundPlayers[countdown.stringId] = soundPlayer
self._delayedSoundPlayers[countdownId] = soundPlayer
FileLogger.log("b) self._delayedSoundPlayers count = \(self._delayedSoundPlayers.count)")
try soundPlayer.start(in: countdown.duration,
repeatCount: Int(countdown.repeatCount))
let dateInterval = DateInterval(start: start, end: end)
self.currentCountdowns[countdown.stringId] = dateInterval
self.currentCountdowns[countdownId] = dateInterval
if Preferences.playConfirmationSound {
self._playConfirmationSound(timer: countdown)
@ -170,6 +171,13 @@ class Conductor: ObservableObject {
}
}
fileprivate func _cleanupCurrentTimerIfNecessary(_ timerId: TimerID) {
self.removeLiveTimer(id: timerId)
if let player = self._delayedSoundPlayers[timerId] {
player.stop() // release resources
}
}
func cancelCountdown(id: TimerID) {
FileLogger.log("Cancel \(id)")

@ -27,15 +27,12 @@ struct LeCountdownApp: App {
Logger.log("path = \(Bundle.main.bundlePath)")
do {
let records = try persistenceController.container.viewContext.fetch(Record.fetchRequest())
for record in records {
Logger.log("duration = \(record.duration)")
}
} catch {
}
// let context = self.persistenceController.container.viewContext
// let records: [Record] = context.fetch(entityName: Record.entityName, sortDescriptor: NSSortDescriptor(key: "start", ascending: false))
// for record in records {
// Logger.log("duration = \(record.duration)")
// }
self._registerBackgroundRefreshes()
// if !ProcessInfo.processInfo.isiOSAppOnMac {

@ -50,12 +50,16 @@ extension NSManagedObjectContext {
// public func fetch<T>(_ request: NSFetchRequest<T>) throws -> [T] where T : NSFetchRequestResult
func fetch<T>(entityName: String, predicate: NSPredicate) -> [T] where T : NSFetchRequestResult {
func fetch<T>(entityName: String, predicate: NSPredicate? = nil, sortDescriptor: NSSortDescriptor? = nil) -> [T] where T : NSFetchRequestResult {
let request = NSFetchRequest<T>(entityName: entityName)
request.predicate = predicate
if let sortDescriptor = sortDescriptor {
request.sortDescriptors = [sortDescriptor]
}
do {
return try self.fetch(request)
} catch {
Logger.error(error)
return []
}
}

@ -63,6 +63,8 @@ import AVFoundation
Logger.log("audioPlayerDidFinishPlaying: successfully = \(flag)")
// Conductor.maestro.cancelSoundPlayer(id: self._timerID)
Conductor.maestro.cleanupLiveActivities()
self.stop()
}
}

@ -76,6 +76,7 @@ enum SoundPlayerError : Error {
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
Conductor.maestro.deactivateAudioSessionIfPossible()
self.stop()
}
func audioPlayerDecodeErrorDidOccur(_ player: AVAudioPlayer, error: Error?) {

Loading…
Cancel
Save