Fix progress updating issues

This commit is contained in:
ronaldheft 2022-08-25 18:28:17 -04:00
parent 268cf67576
commit 7cf36d829a
2 changed files with 29 additions and 23 deletions

View file

@ -144,15 +144,14 @@ class AudioPlayer: NSObject {
let seconds = 0.5 * (self.rate > 0 ? self.rate : 1.0) let seconds = 0.5 * (self.rate > 0 ? self.rate : 1.0)
let time = CMTime(seconds: Double(seconds), preferredTimescale: timeScale) let time = CMTime(seconds: Double(seconds), preferredTimescale: timeScale)
self.timeObserverToken = self.audioPlayer.addPeriodicTimeObserver(forInterval: time, queue: queue) { [weak self] time in self.timeObserverToken = self.audioPlayer.addPeriodicTimeObserver(forInterval: time, queue: queue) { [weak self] time in
let sleepTimeStopAt = self?.sleepTimeStopAt
Task { Task {
// Let the player update the current playback positions // Let the player update the current playback positions
await PlayerProgress.shared.syncFromPlayer(currentTime: time.seconds, includesPlayProgress: true, isStopping: false) await PlayerProgress.shared.syncFromPlayer(currentTime: time.seconds, includesPlayProgress: true, isStopping: false)
}
// Update the sleep time, if set // Update the sleep time, if set
if sleepTimeStopAt != nil { if self?.sleepTimeStopAt != nil {
NotificationCenter.default.post(name: NSNotification.Name(PlayerEvents.sleepSet.rawValue), object: 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) { 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 { if self.audioPlayer.rate != rate {
NSLog("setPlaybakRate rate changed from \(self.audioPlayer.rate) to \(rate)") NSLog("setPlaybakRate rate changed from \(self.audioPlayer.rate) to \(rate)")
self.audioPlayer.rate = 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.rate = rate
self.updateNowPlaying() self.updateNowPlaying()
// If we have an active sleep timer, reschedule based on rate if playbackSpeedChanged {
self.rescheduleSleepTimerAtTime(time: self.getCurrentTime(), secondsRemaining: sleepSecondsRemaining) self.tmpRate = rate
// Setup the time observer again at the new rate // If we have an active sleep timer, reschedule based on rate
self.setupTimeObserver() self.rescheduleSleepTimerAtTime(time: self.getCurrentTime(), secondsRemaining: sleepSecondsRemaining)
// Setup the time observer again at the new rate
self.setupTimeObserver()
}
} }
public func getSleepStopAt() -> Double? { public func getSleepStopAt() -> Double? {

View file

@ -59,12 +59,17 @@ class NowPlayingInfo {
} }
} }
public func update(duration: Double, currentTime: Double, rate: Float) { public func update(duration: Double, currentTime: Double, rate: Float) {
nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = duration // Update on the main to prevent access collisions
nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = currentTime DispatchQueue.main.async { [weak self] in
nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = rate if let self = self {
nowPlayingInfo[MPNowPlayingInfoPropertyDefaultPlaybackRate] = 1.0 self.nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = duration
self.nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = currentTime
self.nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = rate
self.nowPlayingInfo[MPNowPlayingInfoPropertyDefaultPlaybackRate] = 1.0
MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo MPNowPlayingInfoCenter.default().nowPlayingInfo = self.nowPlayingInfo
}
}
} }
public func reset() { public func reset() {