mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2025-08-04 18:15:01 +02:00
Fix player automatically starting after WebKit reload
This commit is contained in:
parent
141877f111
commit
e7f61e34e8
3 changed files with 16 additions and 18 deletions
|
@ -14,7 +14,6 @@ public class AbsAudioPlayer: CAPPlugin {
|
|||
private let logger = AppLogger(category: "AbsAudioPlayer")
|
||||
|
||||
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)
|
||||
|
@ -35,15 +34,14 @@ public class AbsAudioPlayer: CAPPlugin {
|
|||
}
|
||||
|
||||
func restorePlaybackSession() async {
|
||||
// We don't need to restore if we have an active session
|
||||
guard PlayerHandler.getPlaybackSession() == nil else { return }
|
||||
|
||||
do {
|
||||
// Fetch the most recent active session
|
||||
let activeSession = try await Realm().objects(PlaybackSession.self).where({
|
||||
$0.isActiveSession == true && $0.serverConnectionConfigId == Store.serverConfig?.id
|
||||
}).last?.freeze()
|
||||
|
||||
if let activeSession = activeSession {
|
||||
PlayerHandler.stopPlayback(currentSessionId: activeSession.id)
|
||||
await PlayerProgress.shared.syncFromServer()
|
||||
try self.startPlaybackSession(activeSession, playWhenReady: false, playbackRate: PlayerSettings.main().playbackRate)
|
||||
}
|
||||
|
|
|
@ -104,9 +104,6 @@ class AudioPlayer: NSObject {
|
|||
}
|
||||
|
||||
deinit {
|
||||
self.stopPausedTimer()
|
||||
self.removeSleepTimer()
|
||||
self.removeTimeObserver()
|
||||
self.queueObserver?.invalidate()
|
||||
self.queueItemStatusObserver?.invalidate()
|
||||
}
|
||||
|
@ -133,8 +130,13 @@ class AudioPlayer: NSObject {
|
|||
// Remove observers
|
||||
self.audioPlayer.removeObserver(self, forKeyPath: #keyPath(AVPlayer.rate), context: &playerContext)
|
||||
self.audioPlayer.removeObserver(self, forKeyPath: #keyPath(AVPlayer.currentItem), context: &playerContext)
|
||||
self.removeTimeObserver()
|
||||
|
||||
NotificationCenter.default.post(name: NSNotification.Name(PlayerEvents.closed.rawValue), object: nil)
|
||||
|
||||
// Remove timers
|
||||
self.stopPausedTimer()
|
||||
self.removeSleepTimer()
|
||||
}
|
||||
|
||||
public func isInitialized() -> Bool {
|
||||
|
|
|
@ -15,10 +15,7 @@ class PlayerHandler {
|
|||
guard let session = Database.shared.getPlaybackSession(id: sessionId) else { return }
|
||||
|
||||
// Clean up the existing player
|
||||
if player != nil {
|
||||
player?.destroy()
|
||||
player = nil
|
||||
}
|
||||
resetPlayer()
|
||||
|
||||
// Cleanup and sync old sessions
|
||||
cleanupOldSessions(currentSessionId: sessionId)
|
||||
|
@ -31,15 +28,11 @@ class PlayerHandler {
|
|||
player = AudioPlayer(sessionId: sessionId, playWhenReady: playWhenReady, playbackRate: playbackRate)
|
||||
}
|
||||
|
||||
public static func stopPlayback() {
|
||||
public static func stopPlayback(currentSessionId: String? = nil) {
|
||||
// Pause playback first, so we can sync our current progress
|
||||
player?.pause()
|
||||
|
||||
player?.destroy()
|
||||
player = nil
|
||||
|
||||
cleanupOldSessions(currentSessionId: nil)
|
||||
|
||||
resetPlayer()
|
||||
cleanupOldSessions(currentSessionId: currentSessionId)
|
||||
NowPlayingInfo.shared.reset()
|
||||
}
|
||||
|
||||
|
@ -157,4 +150,9 @@ class PlayerHandler {
|
|||
debugPrint(error)
|
||||
}
|
||||
}
|
||||
|
||||
private static func resetPlayer() {
|
||||
player?.destroy()
|
||||
player = nil
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue