mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2026-08-28 14:17:19 +02:00
Restore playback session on startup
This commit is contained in:
@@ -24,10 +24,29 @@ public class AbsAudioPlayer: CAPPlugin {
|
|||||||
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)
|
||||||
|
|
||||||
|
// Restore the playack session when plugin loads
|
||||||
|
Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(restorePlaybackSession), userInfo: nil, repeats: false)
|
||||||
|
|
||||||
self.bridge?.webView?.allowsBackForwardNavigationGestures = true;
|
self.bridge?.webView?.allowsBackForwardNavigationGestures = true;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@objc func restorePlaybackSession() {
|
||||||
|
// 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 Realm().objects(PlaybackSession.self).where({ $0.isActiveSession == true }).last
|
||||||
|
if let activeSession = activeSession {
|
||||||
|
try self.startPlaybackSession(activeSession, playWhenReady: false)
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
NSLog("Failed to restore playback session")
|
||||||
|
debugPrint(error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@objc func startPlaybackSession(_ session: PlaybackSession, playWhenReady: Bool, playbackRate: Float = 1.0) throws {
|
@objc func startPlaybackSession(_ session: PlaybackSession, playWhenReady: Bool, playbackRate: Float = 1.0) throws {
|
||||||
guard let libraryItemId = session.libraryItemId else { throw PlayerError.libraryItemIdNotSpecified }
|
guard let libraryItemId = session.libraryItemId else { throw PlayerError.libraryItemIdNotSpecified }
|
||||||
|
|
||||||
|
|||||||
@@ -69,12 +69,11 @@ class PlayerHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static func cleanupOldSessions(currentSessionId: String?) {
|
private static func cleanupOldSessions(currentSessionId: String?) {
|
||||||
if let currentSessionId = currentSessionId {
|
let realm = try! Realm()
|
||||||
let realm = try! Realm()
|
let oldSessions = realm.objects(PlaybackSession.self) .where({ $0.isActiveSession == true })
|
||||||
let oldSessions = realm.objects(PlaybackSession.self)
|
try! realm.write {
|
||||||
.where({ $0.isActiveSession == true && $0.id != currentSessionId })
|
for s in oldSessions {
|
||||||
try! realm.write {
|
if s.id != currentSessionId {
|
||||||
for s in oldSessions {
|
|
||||||
s.isActiveSession = false
|
s.isActiveSession = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user