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,18 +144,17 @@ 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 {
if self?.sleepTimeStopAt != nil {
NotificationCenter.default.post(name: NSNotification.Name(PlayerEvents.sleepSet.rawValue), object: nil)
}
}
}
}
private func removeTimeObserver() {
if let timeObserverToken = timeObserverToken {
@ -351,26 +350,28 @@ 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 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? {
return self.sleepTimeStopAt

View file

@ -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
// 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 = nowPlayingInfo
MPNowPlayingInfoCenter.default().nowPlayingInfo = self.nowPlayingInfo
}
}
}
public func reset() {