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"> isEnabled = "YES">
</CommandLineArgument> </CommandLineArgument>
</CommandLineArguments> </CommandLineArguments>
<AdditionalOptions>
<AdditionalOption
key = "NSZombieEnabled"
value = "YES"
isEnabled = "YES">
</AdditionalOption>
</AdditionalOptions>
</LaunchAction> </LaunchAction>
<ProfileAction <ProfileAction
buildConfiguration = "Release" buildConfiguration = "Release"

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

@ -136,25 +136,26 @@ class Conductor: ObservableObject {
DispatchQueue.main.async { DispatchQueue.main.async {
let countdownId = countdown.stringId
self._cleanupCurrentTimerIfNecessary(countdownId)
do { do {
let start = Date() let start = Date()
let end = start.addingTimeInterval(countdown.duration) let end = start.addingTimeInterval(countdown.duration)
FileLogger.log("schedule countdown \(countdown.stringId) at \(end)") FileLogger.log("schedule countdown \(countdownId) at \(end)")
self.removeLiveTimer(id: countdown.stringId)
let sound = countdown.someSound 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)") 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)") FileLogger.log("b) self._delayedSoundPlayers count = \(self._delayedSoundPlayers.count)")
try soundPlayer.start(in: countdown.duration, try soundPlayer.start(in: countdown.duration,
repeatCount: Int(countdown.repeatCount)) repeatCount: Int(countdown.repeatCount))
let dateInterval = DateInterval(start: start, end: end) let dateInterval = DateInterval(start: start, end: end)
self.currentCountdowns[countdown.stringId] = dateInterval self.currentCountdowns[countdownId] = dateInterval
if Preferences.playConfirmationSound { if Preferences.playConfirmationSound {
self._playConfirmationSound(timer: countdown) 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) { func cancelCountdown(id: TimerID) {
FileLogger.log("Cancel \(id)") FileLogger.log("Cancel \(id)")

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

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

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

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

Loading…
Cancel
Save