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

View file

@ -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)

View file

@ -9,6 +9,8 @@
#import <Capacitor/Capacitor.h>
CAP_PLUGIN(AbsAudioPlayer, "AbsAudioPlayer",
CAP_PLUGIN_METHOD(onReady, CAPPluginReturnNone);
CAP_PLUGIN_METHOD(prepareLibraryItem, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(closePlayback, CAPPluginReturnPromise);

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(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")

View file

@ -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) {

View file

@ -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

View file

@ -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"
}

View file

@ -279,6 +279,9 @@ export default {
this.loadSavedSettings()
this.hasMounted = true
console.log('[default] fully initialized')
this.$eventBus.$emit('abs-ui-ready')
}
},
beforeDestroy() {