diff --git a/components/app/AudioPlayerContainer.vue b/components/app/AudioPlayerContainer.vue index e7b7d282..14040414 100644 --- a/components/app/AudioPlayerContainer.vue +++ b/components/app/AudioPlayerContainer.vue @@ -15,6 +15,8 @@ import { Dialog } from '@capacitor/dialog' export default { data() { return { + isReady: false, + settingsLoaded: false, audioPlayerReady: false, stream: null, download: null, @@ -151,6 +153,10 @@ export default { console.log(`[AudioPlayerContainer] PlaybackRate Updated: ${this.playbackSpeed}`) this.$refs.audioPlayer.setPlaybackSpeed(this.playbackSpeed) } + + // Settings have been loaded (at least once, so it's safe to kickoff onReady) + this.settingsLoaded = true + this.notifyOnReady() }, setListeners() { // if (!this.$server.socket) { @@ -261,6 +267,18 @@ export default { onMediaPlayerChanged(data) { var mediaPlayer = data.value this.$store.commit('setMediaPlayer', mediaPlayer) + }, + onReady() { + // The UI is reporting elsewhere we are ready + this.isReady = true + this.notifyOnReady() + }, + notifyOnReady() { + // If settings aren't loaded yet, native player will receive incorrect settings + console.log('Notify on ready... settingsLoaded:', this.settingsLoaded, 'isReady:', this.isReady) + if ( this.settingsLoaded && this.isReady ) { + AbsAudioPlayer.onReady() + } } }, mounted() { @@ -273,6 +291,7 @@ export default { console.log(`[AudioPlayerContainer] Init Playback Speed: ${this.playbackSpeed}`) this.setListeners() + this.$eventBus.$on('abs-ui-ready', this.onReady) this.$eventBus.$on('play-item', this.playLibraryItem) this.$eventBus.$on('pause-item', this.pauseItem) this.$eventBus.$on('close-stream', this.closeStreamOnly) @@ -292,6 +311,7 @@ export default { // this.$server.socket.off('stream_ready', this.streamReady) // this.$server.socket.off('stream_reset', this.streamReset) // } + this.$eventBus.$off('abs-ui-ready', this.onReady) this.$eventBus.$off('play-item', this.playLibraryItem) this.$eventBus.$off('pause-item', this.pauseItem) this.$eventBus.$off('close-stream', this.closeStreamOnly) diff --git a/ios/App/App/plugins/AbsAudioPlayer.m b/ios/App/App/plugins/AbsAudioPlayer.m index 95e705b4..49aaff7a 100644 --- a/ios/App/App/plugins/AbsAudioPlayer.m +++ b/ios/App/App/plugins/AbsAudioPlayer.m @@ -9,6 +9,8 @@ #import CAP_PLUGIN(AbsAudioPlayer, "AbsAudioPlayer", + CAP_PLUGIN_METHOD(onReady, CAPPluginReturnNone); + CAP_PLUGIN_METHOD(prepareLibraryItem, CAPPluginReturnPromise); CAP_PLUGIN_METHOD(closePlayback, CAPPluginReturnPromise); diff --git a/ios/App/App/plugins/AbsAudioPlayer.swift b/ios/App/App/plugins/AbsAudioPlayer.swift index 9ffa975e..7272d3bf 100644 --- a/ios/App/App/plugins/AbsAudioPlayer.swift +++ b/ios/App/App/plugins/AbsAudioPlayer.swift @@ -23,18 +23,13 @@ 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) - NotificationCenter.default.addObserver(self, selector: #selector(userInterfaceReady), name: NSNotification.Name(PlayerEvents.playerUserInterfaceReady.rawValue), object: nil) self.bridge?.webView?.allowsBackForwardNavigationGestures = true; } - @objc func userInterfaceReady() { - if !self.isUIReady { - NSLog("User interface is ready. Performing state restore...") - self.restorePlaybackSession() - self.isUIReady = true - } + @objc func onReady(_ call: CAPPluginCall) { + self.restorePlaybackSession() } func restorePlaybackSession() { @@ -46,7 +41,7 @@ public class AbsAudioPlayer: CAPPlugin { let activeSession = try Realm().objects(PlaybackSession.self).where({ $0.isActiveSession == true }).last if let activeSession = activeSession { try self.startPlaybackSession(activeSession, playWhenReady: false, playbackRate: PlayerSettings.main().playbackRate) - PlayerHandler.syncServerProgressDuringPause() + //PlayerHandler.syncServerProgressDuringPause() } } catch { NSLog("Failed to restore playback session") diff --git a/ios/App/App/plugins/AbsDatabase.swift b/ios/App/App/plugins/AbsDatabase.swift index 4c5cf2a4..a78619df 100644 --- a/ios/App/App/plugins/AbsDatabase.swift +++ b/ios/App/App/plugins/AbsDatabase.swift @@ -155,10 +155,6 @@ 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) { diff --git a/ios/App/Shared/player/AudioPlayer.swift b/ios/App/Shared/player/AudioPlayer.swift index 857fa140..3ba2da62 100644 --- a/ios/App/Shared/player/AudioPlayer.swift +++ b/ios/App/Shared/player/AudioPlayer.swift @@ -261,12 +261,6 @@ 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 diff --git a/ios/App/Shared/util/PlayerEvents.swift b/ios/App/Shared/util/PlayerEvents.swift index d8bfa0d1..4b225a9e 100644 --- a/ios/App/Shared/util/PlayerEvents.swift +++ b/ios/App/Shared/util/PlayerEvents.swift @@ -14,5 +14,4 @@ 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" } diff --git a/layouts/default.vue b/layouts/default.vue index 037ad41b..19a0d4e1 100644 --- a/layouts/default.vue +++ b/layouts/default.vue @@ -279,6 +279,9 @@ export default { this.loadSavedSettings() this.hasMounted = true + + console.log('[default] fully initialized') + this.$eventBus.$emit('abs-ui-ready') } }, beforeDestroy() {