mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2025-08-04 10:04:39 +02:00
Fix race conditions on first launch
This commit is contained in:
parent
af835f2c43
commit
bf46c46fc0
4 changed files with 22 additions and 4 deletions
|
@ -12,6 +12,7 @@ import RealmSwift
|
|||
@objc(AbsAudioPlayer)
|
||||
public class AbsAudioPlayer: CAPPlugin {
|
||||
private var initialPlayWhenReady = false
|
||||
private var isUIReady = false
|
||||
|
||||
override public func load() {
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(sendMetadata), name: NSNotification.Name(PlayerEvents.update.rawValue), object: nil)
|
||||
|
@ -22,15 +23,21 @@ public class AbsAudioPlayer: CAPPlugin {
|
|||
NotificationCenter.default.addObserver(self, selector: #selector(sendSleepTimerEnded), name: NSNotification.Name(PlayerEvents.sleepEnded.rawValue), object: nil)
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(onPlaybackFailed), name: NSNotification.Name(PlayerEvents.failed.rawValue), object: nil)
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(onLocalMediaProgressUpdate), name: NSNotification.Name(PlayerEvents.localProgress.rawValue), object: nil)
|
||||
|
||||
// Restore the playack session when plugin loads
|
||||
Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(restorePlaybackSession), userInfo: nil, repeats: false)
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(userInterfaceReady), name: NSNotification.Name(PlayerEvents.playerUserInterfaceReady.rawValue), object: nil)
|
||||
|
||||
self.bridge?.webView?.allowsBackForwardNavigationGestures = true;
|
||||
|
||||
}
|
||||
|
||||
@objc func restorePlaybackSession() {
|
||||
@objc func userInterfaceReady() {
|
||||
if !self.isUIReady {
|
||||
NSLog("User interface is ready. Performing state restore...")
|
||||
self.restorePlaybackSession()
|
||||
self.isUIReady = true
|
||||
}
|
||||
}
|
||||
|
||||
func restorePlaybackSession() {
|
||||
// We don't need to restore if we have an active session
|
||||
guard PlayerHandler.getPlaybackSession() == nil else { return }
|
||||
|
||||
|
|
|
@ -155,6 +155,10 @@ public class AbsDatabase: CAPPlugin {
|
|||
call.reject("Failed to report synced media progress")
|
||||
}
|
||||
}
|
||||
|
||||
// If we're syncing progress with the server, we also have the UI ready
|
||||
// This will restore the player to the last playback session
|
||||
NotificationCenter.default.post(name: NSNotification.Name(PlayerEvents.playerUserInterfaceReady.rawValue), object: nil)
|
||||
}
|
||||
|
||||
@objc func syncServerMediaProgressWithLocalMediaProgress(_ call: CAPPluginCall) {
|
||||
|
|
|
@ -261,6 +261,12 @@ class AudioPlayer: NSObject {
|
|||
}
|
||||
|
||||
public func setPlaybackRate(_ rate: Float, observed: Bool = false) {
|
||||
// Handle a race condition on first launch
|
||||
guard self.status != -1 else {
|
||||
NSLog("Did not set playback rate as player is not initialized")
|
||||
return
|
||||
}
|
||||
|
||||
if self.audioPlayer.rate != rate {
|
||||
NSLog("setPlaybakRate rate changed from \(self.audioPlayer.rate) to \(rate)")
|
||||
self.audioPlayer.rate = rate
|
||||
|
|
|
@ -14,4 +14,5 @@ enum PlayerEvents: String {
|
|||
case sleepEnded = "com.audiobookshelf.app.player.sleep.ended"
|
||||
case failed = "com.audiobookshelf.app.player.failed"
|
||||
case localProgress = "com.audiobookshelf.app.player.localProgress"
|
||||
case playerUserInterfaceReady = "com.audiobookshelf.app.player.playerUserInterfaceReady"
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue