Fix player session restoration events

This commit is contained in:
ronaldheft
2022-08-19 16:36:56 -04:00
parent bf46c46fc0
commit 93cb27d324
7 changed files with 28 additions and 19 deletions
+20
View File
@@ -15,6 +15,8 @@ import { Dialog } from '@capacitor/dialog'
export default { export default {
data() { data() {
return { return {
isReady: false,
settingsLoaded: false,
audioPlayerReady: false, audioPlayerReady: false,
stream: null, stream: null,
download: null, download: null,
@@ -151,6 +153,10 @@ export default {
console.log(`[AudioPlayerContainer] PlaybackRate Updated: ${this.playbackSpeed}`) console.log(`[AudioPlayerContainer] PlaybackRate Updated: ${this.playbackSpeed}`)
this.$refs.audioPlayer.setPlaybackSpeed(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() { setListeners() {
// if (!this.$server.socket) { // if (!this.$server.socket) {
@@ -261,6 +267,18 @@ export default {
onMediaPlayerChanged(data) { onMediaPlayerChanged(data) {
var mediaPlayer = data.value var mediaPlayer = data.value
this.$store.commit('setMediaPlayer', mediaPlayer) 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() { mounted() {
@@ -273,6 +291,7 @@ export default {
console.log(`[AudioPlayerContainer] Init Playback Speed: ${this.playbackSpeed}`) console.log(`[AudioPlayerContainer] Init Playback Speed: ${this.playbackSpeed}`)
this.setListeners() this.setListeners()
this.$eventBus.$on('abs-ui-ready', this.onReady)
this.$eventBus.$on('play-item', this.playLibraryItem) this.$eventBus.$on('play-item', this.playLibraryItem)
this.$eventBus.$on('pause-item', this.pauseItem) this.$eventBus.$on('pause-item', this.pauseItem)
this.$eventBus.$on('close-stream', this.closeStreamOnly) 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_ready', this.streamReady)
// this.$server.socket.off('stream_reset', this.streamReset) // 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('play-item', this.playLibraryItem)
this.$eventBus.$off('pause-item', this.pauseItem) this.$eventBus.$off('pause-item', this.pauseItem)
this.$eventBus.$off('close-stream', this.closeStreamOnly) this.$eventBus.$off('close-stream', this.closeStreamOnly)
+2
View File
@@ -9,6 +9,8 @@
#import <Capacitor/Capacitor.h> #import <Capacitor/Capacitor.h>
CAP_PLUGIN(AbsAudioPlayer, "AbsAudioPlayer", CAP_PLUGIN(AbsAudioPlayer, "AbsAudioPlayer",
CAP_PLUGIN_METHOD(onReady, CAPPluginReturnNone);
CAP_PLUGIN_METHOD(prepareLibraryItem, CAPPluginReturnPromise); CAP_PLUGIN_METHOD(prepareLibraryItem, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(closePlayback, CAPPluginReturnPromise); CAP_PLUGIN_METHOD(closePlayback, CAPPluginReturnPromise);
+3 -8
View File
@@ -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(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(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(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; self.bridge?.webView?.allowsBackForwardNavigationGestures = true;
} }
@objc func userInterfaceReady() { @objc func onReady(_ call: CAPPluginCall) {
if !self.isUIReady { self.restorePlaybackSession()
NSLog("User interface is ready. Performing state restore...")
self.restorePlaybackSession()
self.isUIReady = true
}
} }
func restorePlaybackSession() { func restorePlaybackSession() {
@@ -46,7 +41,7 @@ public class AbsAudioPlayer: CAPPlugin {
let activeSession = try Realm().objects(PlaybackSession.self).where({ $0.isActiveSession == true }).last let activeSession = try Realm().objects(PlaybackSession.self).where({ $0.isActiveSession == true }).last
if let activeSession = activeSession { if let activeSession = activeSession {
try self.startPlaybackSession(activeSession, playWhenReady: false, playbackRate: PlayerSettings.main().playbackRate) try self.startPlaybackSession(activeSession, playWhenReady: false, playbackRate: PlayerSettings.main().playbackRate)
PlayerHandler.syncServerProgressDuringPause() //PlayerHandler.syncServerProgressDuringPause()
} }
} catch { } catch {
NSLog("Failed to restore playback session") NSLog("Failed to restore playback session")
-4
View File
@@ -155,10 +155,6 @@ public class AbsDatabase: CAPPlugin {
call.reject("Failed to report synced media progress") 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) { @objc func syncServerMediaProgressWithLocalMediaProgress(_ call: CAPPluginCall) {
-6
View File
@@ -261,12 +261,6 @@ class AudioPlayer: NSObject {
} }
public func setPlaybackRate(_ rate: Float, observed: Bool = false) { 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 { if self.audioPlayer.rate != rate {
NSLog("setPlaybakRate rate changed from \(self.audioPlayer.rate) to \(rate)") NSLog("setPlaybakRate rate changed from \(self.audioPlayer.rate) to \(rate)")
self.audioPlayer.rate = rate self.audioPlayer.rate = rate
-1
View File
@@ -14,5 +14,4 @@ enum PlayerEvents: String {
case sleepEnded = "com.audiobookshelf.app.player.sleep.ended" case sleepEnded = "com.audiobookshelf.app.player.sleep.ended"
case failed = "com.audiobookshelf.app.player.failed" case failed = "com.audiobookshelf.app.player.failed"
case localProgress = "com.audiobookshelf.app.player.localProgress" case localProgress = "com.audiobookshelf.app.player.localProgress"
case playerUserInterfaceReady = "com.audiobookshelf.app.player.playerUserInterfaceReady"
} }
+3
View File
@@ -279,6 +279,9 @@ export default {
this.loadSavedSettings() this.loadSavedSettings()
this.hasMounted = true this.hasMounted = true
console.log('[default] fully initialized')
this.$eventBus.$emit('abs-ui-ready')
} }
}, },
beforeDestroy() { beforeDestroy() {