From 7cf36d829a75cc183626789ceed4b643ee5527c3 Mon Sep 17 00:00:00 2001 From: ronaldheft Date: Thu, 25 Aug 2022 18:28:17 -0400 Subject: [PATCH] Fix progress updating issues --- ios/App/Shared/player/AudioPlayer.swift | 35 ++++++++++++------------ ios/App/Shared/util/NowPlayingInfo.swift | 17 ++++++++---- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/ios/App/Shared/player/AudioPlayer.swift b/ios/App/Shared/player/AudioPlayer.swift index ac2585ed..b002b3ad 100644 --- a/ios/App/Shared/player/AudioPlayer.swift +++ b/ios/App/Shared/player/AudioPlayer.swift @@ -144,15 +144,14 @@ class AudioPlayer: NSObject { let seconds = 0.5 * (self.rate > 0 ? self.rate : 1.0) let time = CMTime(seconds: Double(seconds), preferredTimescale: timeScale) self.timeObserverToken = self.audioPlayer.addPeriodicTimeObserver(forInterval: time, queue: queue) { [weak self] time in - let sleepTimeStopAt = self?.sleepTimeStopAt Task { // Let the player update the current playback positions await PlayerProgress.shared.syncFromPlayer(currentTime: time.seconds, includesPlayProgress: true, isStopping: false) - - // Update the sleep time, if set - if sleepTimeStopAt != nil { - NotificationCenter.default.post(name: NSNotification.Name(PlayerEvents.sleepSet.rawValue), object: nil) - } + } + + // Update the sleep time, if set + if self?.sleepTimeStopAt != nil { + NotificationCenter.default.post(name: NSNotification.Name(PlayerEvents.sleepSet.rawValue), object: nil) } } } @@ -351,25 +350,27 @@ class AudioPlayer: NSObject { } public func setPlaybackRate(_ rate: Float, observed: Bool = false) { + // Capture remaining sleep time before changing the rate + let sleepSecondsRemaining = PlayerHandler.remainingSleepTime + let playbackSpeedChanged = rate > 0.0 && rate != self.tmpRate && !(observed && rate == 1) + if self.audioPlayer.rate != rate { NSLog("setPlaybakRate rate changed from \(self.audioPlayer.rate) to \(rate)") self.audioPlayer.rate = rate } - if rate > 0.0 && !(observed && rate == 1) { - self.tmpRate = rate - } - - // Capture remaining sleep time before changing the rate - let sleepSecondsRemaining = PlayerHandler.remainingSleepTime self.rate = rate self.updateNowPlaying() - // If we have an active sleep timer, reschedule based on rate - self.rescheduleSleepTimerAtTime(time: self.getCurrentTime(), secondsRemaining: sleepSecondsRemaining) - - // Setup the time observer again at the new rate - self.setupTimeObserver() + if playbackSpeedChanged { + self.tmpRate = rate + + // If we have an active sleep timer, reschedule based on rate + self.rescheduleSleepTimerAtTime(time: self.getCurrentTime(), secondsRemaining: sleepSecondsRemaining) + + // Setup the time observer again at the new rate + self.setupTimeObserver() + } } public func getSleepStopAt() -> Double? { diff --git a/ios/App/Shared/util/NowPlayingInfo.swift b/ios/App/Shared/util/NowPlayingInfo.swift index 01e6d386..387c8911 100644 --- a/ios/App/Shared/util/NowPlayingInfo.swift +++ b/ios/App/Shared/util/NowPlayingInfo.swift @@ -59,12 +59,17 @@ class NowPlayingInfo { } } public func update(duration: Double, currentTime: Double, rate: Float) { - nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = duration - nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = currentTime - nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = rate - nowPlayingInfo[MPNowPlayingInfoPropertyDefaultPlaybackRate] = 1.0 - - MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo + // Update on the main to prevent access collisions + DispatchQueue.main.async { [weak self] in + if let self = self { + self.nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = duration + self.nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = currentTime + self.nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = rate + self.nowPlayingInfo[MPNowPlayingInfoPropertyDefaultPlaybackRate] = 1.0 + + MPNowPlayingInfoCenter.default().nowPlayingInfo = self.nowPlayingInfo + } + } } public func reset() {