mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2025-08-30 14:49:47 +02:00
Fix race condition with the player queue
This commit is contained in:
parent
defd895995
commit
6257c6488b
1 changed files with 39 additions and 27 deletions
|
@ -195,14 +195,27 @@ class AudioPlayer: NSObject {
|
||||||
|
|
||||||
private func setupQueueItemStatusObserver() {
|
private func setupQueueItemStatusObserver() {
|
||||||
self.queueItemStatusObserver?.invalidate()
|
self.queueItemStatusObserver?.invalidate()
|
||||||
self.queueItemStatusObserver = self.audioPlayer.currentItem?.observe(\.status, options: [.new, .old], changeHandler: { (playerItem, change) in
|
let status = self.audioPlayer.currentItem?.status.rawValue ?? -1
|
||||||
|
|
||||||
|
NSLog("queueStatusObserver: Setting up status=\(status)")
|
||||||
|
// First item already loaded, we need to fire manually
|
||||||
|
if status == 1, let playerItem = self.audioPlayer.currentItem {
|
||||||
|
self.handleQueueItemStatus(playerItem: playerItem)
|
||||||
|
}
|
||||||
|
// Now listen for future updates
|
||||||
|
self.queueItemStatusObserver = self.audioPlayer.currentItem?.observe(\.status, options: [.new, .old], changeHandler: { playerItem, change in
|
||||||
|
self.handleQueueItemStatus(playerItem: playerItem)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
private func handleQueueItemStatus(playerItem: AVPlayerItem) {
|
||||||
|
NSLog("queueStatusObserver: Current item status changed")
|
||||||
guard let playbackSession = self.getPlaybackSession() else {
|
guard let playbackSession = self.getPlaybackSession() else {
|
||||||
NotificationCenter.default.post(name: NSNotification.Name(PlayerEvents.failed.rawValue), object: nil)
|
NotificationCenter.default.post(name: NSNotification.Name(PlayerEvents.failed.rawValue), object: nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if (playerItem.status == .readyToPlay) {
|
if (playerItem.status == .readyToPlay) {
|
||||||
NSLog("queueStatusObserver: Current Item Ready to play. PlayWhenReady: \(self.playWhenReady)")
|
NSLog("queueStatusObserver: Current Item Ready to play. PlayWhenReady: \(self.playWhenReady)")
|
||||||
self.updateNowPlaying()
|
|
||||||
|
|
||||||
// Seek the player before initializing, so a currentTime of 0 does not appear in MediaProgress / session
|
// Seek the player before initializing, so a currentTime of 0 does not appear in MediaProgress / session
|
||||||
let firstReady = self.status < 0
|
let firstReady = self.status < 0
|
||||||
|
@ -222,7 +235,6 @@ class AudioPlayer: NSObject {
|
||||||
NSLog("queueStatusObserver: FAILED \(playerItem.error?.localizedDescription ?? "")")
|
NSLog("queueStatusObserver: FAILED \(playerItem.error?.localizedDescription ?? "")")
|
||||||
NotificationCenter.default.post(name: NSNotification.Name(PlayerEvents.failed.rawValue), object: nil)
|
NotificationCenter.default.post(name: NSNotification.Name(PlayerEvents.failed.rawValue), object: nil)
|
||||||
}
|
}
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private func startPausedTimer() {
|
private func startPausedTimer() {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue