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