advplyr.audiobookshelf-app/ios/App/Shared/player/PlayerHandler.swift

168 lines
5.2 KiB
Swift
Raw Normal View History

2022-04-11 16:29:19 +02:00
//
// PlayerHandler.swift
// App
//
// Created by Rasmus Krämer on 11.04.22.
//
import Foundation
2022-08-16 12:32:22 -04:00
import RealmSwift
2022-04-11 16:29:19 +02:00
class PlayerHandler {
private static var player: AudioPlayer?
2022-04-15 12:21:46 +02:00
2022-08-16 12:32:22 -04:00
public static func startPlayback(sessionId: String, playWhenReady: Bool, playbackRate: Float) {
guard let session = Database.shared.getPlaybackSession(id: sessionId) else { return }
2022-08-16 20:45:29 -04:00
// Clean up the existing player
resetPlayer()
2022-08-22 17:04:48 -04:00
// Cleanup and sync old sessions
2022-08-16 20:45:29 -04:00
cleanupOldSessions(currentSessionId: sessionId)
// Set now playing info
NowPlayingInfo.shared.setSessionMetadata(metadata: NowPlayingMetadata(id: session.id, itemId: session.libraryItemId!, title: session.displayTitle ?? "Unknown title", author: session.displayAuthor, series: nil, isLocal: session.isLocal))
2022-04-15 12:21:46 +02:00
2022-08-16 20:45:29 -04:00
// Create the audio player
2022-08-16 12:32:22 -04:00
player = AudioPlayer(sessionId: sessionId, playWhenReady: playWhenReady, playbackRate: playbackRate)
2022-04-11 16:29:19 +02:00
}
public static func stopPlayback(currentSessionId: String? = nil) {
2022-08-13 12:41:20 -04:00
// Pause playback first, so we can sync our current progress
player?.pause()
resetPlayer()
cleanupOldSessions(currentSessionId: currentSessionId)
2022-05-03 12:55:13 +02:00
NowPlayingInfo.shared.reset()
2022-04-14 14:39:09 +02:00
}
2022-08-22 17:04:48 -04:00
public static var paused: Bool {
get {
guard let player = player else { return true }
return player.rateManager.rate == 0.0
2022-08-22 17:04:48 -04:00
}
set(paused) {
if paused {
self.player?.pause()
} else {
2022-09-18 12:43:52 -04:00
self.player?.play(allowSeekBack: true)
2022-08-22 17:04:48 -04:00
}
}
}
2022-04-14 14:39:09 +02:00
public static func getCurrentTime() -> Double? {
self.player?.getCurrentTime()
}
2023-02-19 11:47:05 -06:00
public static func getPlayWhenReady() -> Bool {
self.player?.playWhenReady ?? false
}
2022-04-14 14:39:09 +02:00
public static func setPlaybackSpeed(speed: Float) {
self.player?.setPlaybackRate(speed)
}
2023-12-22 21:01:37 -05:00
public static func setChapterTrack() {
self.player?.setChapterTrack()
}
2022-09-02 16:31:47 -04:00
public static func getSleepTimeRemaining() -> Double? {
return self.player?.getSleepTimeRemaining()
}
2022-08-22 17:04:48 -04:00
public static func setSleepTime(secondsUntilSleep: Double) {
2022-09-02 18:22:42 -04:00
self.player?.setSleepTimer(secondsUntilSleep: secondsUntilSleep)
2022-08-22 17:04:48 -04:00
}
public static func setChapterSleepTime(stopAt: Double) {
2022-09-02 18:22:42 -04:00
self.player?.setChapterSleepTimer(stopAt: stopAt)
2022-08-22 17:04:48 -04:00
}
public static func increaseSleepTime(increaseSeconds: Double) {
self.player?.increaseSleepTime(extraTimeInSeconds: increaseSeconds)
}
public static func decreaseSleepTime(decreaseSeconds: Double) {
self.player?.decreaseSleepTime(removeTimeInSeconds: decreaseSeconds)
}
public static func cancelSleepTime() {
self.player?.removeSleepTimer()
}
public static func getPlayMethod() -> Int? {
self.player?.getPlayMethod()
}
public static func getPlaybackSession() -> PlaybackSession? {
2022-08-16 12:32:22 -04:00
guard let player = player else { return nil }
2023-02-19 11:47:05 -06:00
return player.getPlaybackSession()
}
2022-04-14 14:39:09 +02:00
public static func seekForward(amount: Double) {
2022-08-21 12:36:29 -04:00
guard let player = player else { return }
guard player.isInitialized() else { return }
2022-08-30 17:05:06 -04:00
guard let currentTime = player.getCurrentTime() else { return }
2022-04-14 14:39:09 +02:00
2022-08-30 17:05:06 -04:00
let destinationTime = currentTime + amount
player.seek(destinationTime, from: "handler")
2022-04-14 14:39:09 +02:00
}
2022-04-14 14:39:09 +02:00
public static func seekBackward(amount: Double) {
2022-08-21 12:36:29 -04:00
guard let player = player else { return }
guard player.isInitialized() else { return }
2022-08-30 17:05:06 -04:00
guard let currentTime = player.getCurrentTime() else { return }
2022-04-14 14:39:09 +02:00
2022-08-30 17:05:06 -04:00
let destinationTime = currentTime - amount
player.seek(destinationTime, from: "handler")
2022-04-14 14:39:09 +02:00
}
2022-04-14 14:39:09 +02:00
public static func seek(amount: Double) {
guard let player = player else { return }
guard player.isInitialized() else { return }
player.seek(amount, from: "handler")
2022-04-14 14:39:09 +02:00
}
2023-02-09 02:38:56 +01:00
public static func getMetdata() -> PlaybackMetadata? {
2022-08-16 21:14:33 -04:00
guard let player = player else { return nil }
guard player.isInitialized() else { return nil }
2023-02-09 02:38:56 +01:00
return PlaybackMetadata(
duration: player.getDuration() ?? 0,
currentTime: player.getCurrentTime() ?? 0,
playerState: player.getPlayerState()
)
2022-04-14 14:39:09 +02:00
}
2022-04-15 12:21:46 +02:00
public static func updateRemoteTransportControls() {
self.player?.setupRemoteTransportControls()
}
2022-08-22 17:04:48 -04:00
// MARK: - Helper logic
private static func cleanupOldSessions(currentSessionId: String?) {
2022-08-25 15:42:37 -04:00
do {
let realm = try Realm()
let oldSessions = realm.objects(PlaybackSession.self) .where({
$0.isActiveSession == true && $0.serverConnectionConfigId == Store.serverConfig?.id
})
try realm.write {
for s in oldSessions {
if s.id != currentSessionId {
s.isActiveSession = false
}
2022-08-11 18:49:04 -05:00
}
}
2022-08-25 15:42:37 -04:00
} catch {
debugPrint("Failed to cleanup sessions")
debugPrint(error)
2022-04-15 12:21:46 +02:00
}
}
private static func resetPlayer() {
player?.destroy()
player = nil
}
2022-04-11 16:29:19 +02:00
}