mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2025-08-05 10:35:42 +02:00
Configure time observer on the main queue
This commit is contained in:
parent
7cf36d829a
commit
3e31e72734
1 changed files with 17 additions and 14 deletions
|
@ -137,21 +137,24 @@ class AudioPlayer: NSObject {
|
|||
}
|
||||
|
||||
private func setupTimeObserver() {
|
||||
removeTimeObserver()
|
||||
|
||||
let timeScale = CMTimeScale(NSEC_PER_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 time = CMTime(seconds: Double(seconds), preferredTimescale: timeScale)
|
||||
self.timeObserverToken = self.audioPlayer.addPeriodicTimeObserver(forInterval: time, queue: queue) { [weak self] time in
|
||||
Task {
|
||||
// Let the player update the current playback positions
|
||||
await PlayerProgress.shared.syncFromPlayer(currentTime: time.seconds, includesPlayProgress: true, isStopping: false)
|
||||
}
|
||||
// Time observer should be configured on the main queue
|
||||
DispatchQueue.main.sync {
|
||||
self.removeTimeObserver()
|
||||
|
||||
// Update the sleep time, if set
|
||||
if self?.sleepTimeStopAt != nil {
|
||||
NotificationCenter.default.post(name: NSNotification.Name(PlayerEvents.sleepSet.rawValue), object: nil)
|
||||
let timeScale = CMTimeScale(NSEC_PER_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 time = CMTime(seconds: Double(seconds), preferredTimescale: timeScale)
|
||||
self.timeObserverToken = self.audioPlayer.addPeriodicTimeObserver(forInterval: time, queue: queue) { [weak self] time in
|
||||
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 self?.sleepTimeStopAt != nil {
|
||||
NotificationCenter.default.post(name: NSNotification.Name(PlayerEvents.sleepSet.rawValue), object: nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue