Make timer easier to start for the future

This commit is contained in:
ronaldheft
2022-08-13 12:56:02 -04:00
parent e0e2f02e0b
commit 1f8a8d1ce5
2 changed files with 22 additions and 7 deletions
+18 -7
View File
@@ -52,6 +52,22 @@ class PlayerHandler {
}
}
public static func startTickTimer() {
DispatchQueue.runOnMainQueue {
NSLog("Starting the tick timer")
timer?.invalidate()
timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { _ in
self.tick()
}
}
}
public static func stopTickTimer() {
NSLog("Stopping the tick timer")
timer?.invalidate()
timer = nil
}
public static func startPlayback(session: PlaybackSession, playWhenReady: Bool, playbackRate: Float) {
if player != nil {
player?.destroy()
@@ -63,11 +79,7 @@ class PlayerHandler {
self.session = session
player = AudioPlayer(playbackSession: session, playWhenReady: playWhenReady, playbackRate: playbackRate)
DispatchQueue.runOnMainQueue {
timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { _ in
self.tick()
}
}
startTickTimer()
}
public static func stopPlayback() {
@@ -75,8 +87,7 @@ class PlayerHandler {
player?.pause()
// Stop updating progress before we destory the player, so we don't receive bad data
timer?.invalidate()
timer = nil
stopTickTimer()
player?.destroy()
player = nil