mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2026-08-30 15:17:14 +02:00
Configure time observer on the main queue
This commit is contained in:
@@ -137,21 +137,24 @@ class AudioPlayer: NSObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private func setupTimeObserver() {
|
private func setupTimeObserver() {
|
||||||
removeTimeObserver()
|
// Time observer should be configured on the main queue
|
||||||
|
DispatchQueue.main.sync {
|
||||||
|
self.removeTimeObserver()
|
||||||
|
|
||||||
let timeScale = CMTimeScale(NSEC_PER_SEC)
|
let timeScale = CMTimeScale(NSEC_PER_SEC)
|
||||||
// Rate will be different depending on playback speed, aim for 2 observations/sec
|
// Rate will be different depending on playback speed, aim for 2 observations/sec
|
||||||
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
|
||||||
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 self?.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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user