Re-implement the paused timer

This commit is contained in:
ronaldheft 2022-08-22 18:00:37 -04:00
parent fe042f3f83
commit 2448b461f0
2 changed files with 22 additions and 4 deletions

View file

@ -44,6 +44,8 @@ class AudioPlayer: NSObject {
private var currentTrackIndex = 0 private var currentTrackIndex = 0
private var allPlayerItems:[AVPlayerItem] = [] private var allPlayerItems:[AVPlayerItem] = []
private var pausedTimer: Timer?
// MARK: - Constructor // MARK: - Constructor
init(sessionId: String, playWhenReady: Bool = false, playbackRate: Float = 1) { init(sessionId: String, playWhenReady: Bool = false, playbackRate: Float = 1) {
self.playWhenReady = playWhenReady self.playWhenReady = playWhenReady
@ -88,6 +90,7 @@ class AudioPlayer: NSObject {
NSLog("Audioplayer ready") NSLog("Audioplayer ready")
} }
deinit { deinit {
self.stopPausedTimer()
self.removeSleepTimer() self.removeSleepTimer()
self.removeTimeObserver() self.removeTimeObserver()
self.queueObserver?.invalidate() self.queueObserver?.invalidate()
@ -200,6 +203,21 @@ class AudioPlayer: NSObject {
}) })
} }
private func startPausedTimer() {
guard self.pausedTimer == nil else { return }
DispatchQueue.main.async {
self.pausedTimer = Timer.scheduledTimer(withTimeInterval: 10, repeats: true) { timer in
NSLog("PAUSE TIMER: Syncing from server")
Task { await PlayerProgress.shared.syncFromServer() }
}
}
}
private func stopPausedTimer() {
self.pausedTimer?.invalidate()
self.pausedTimer = nil
}
// MARK: - Methods // MARK: - Methods
public func play(allowSeekBack: Bool = false) { public func play(allowSeekBack: Bool = false) {
if allowSeekBack { if allowSeekBack {
@ -228,6 +246,8 @@ class AudioPlayer: NSObject {
} }
lastPlayTime = Date.timeIntervalSinceReferenceDate lastPlayTime = Date.timeIntervalSinceReferenceDate
self.stopPausedTimer()
Task { Task {
let isPlaying = self.status > 0 let isPlaying = self.status > 0
await PlayerProgress.shared.syncFromPlayer(currentTime: self.getCurrentTime(), includesPlayProgress: isPlaying, isStopping: false) await PlayerProgress.shared.syncFromPlayer(currentTime: self.getCurrentTime(), includesPlayProgress: isPlaying, isStopping: false)
@ -252,6 +272,8 @@ class AudioPlayer: NSObject {
updateNowPlaying() updateNowPlaying()
lastPlayTime = Date.timeIntervalSinceReferenceDate lastPlayTime = Date.timeIntervalSinceReferenceDate
self.startPausedTimer()
} }
public func seek(_ to: Double, from: String) { public func seek(_ to: Double, from: String) {

View file

@ -168,8 +168,4 @@ class PlayerHandler {
} }
} }
} }
@objc public static func syncServerProgressDuringPause() {
Task { await PlayerProgress.shared.syncFromServer() }
}
} }