feat: now playing chapter track

This commit is contained in:
benonymity
2023-12-22 21:01:37 -05:00
parent 793f0c05f7
commit 2b1667e532
11 changed files with 91 additions and 11 deletions
+16 -1
View File
@@ -224,6 +224,8 @@ class AudioPlayer: NSObject {
// Update the UI
NotificationCenter.default.post(name: NSNotification.Name(PlayerEvents.sleepSet.rawValue), object: nil)
}
// Update the now playing and chapter info
self.updateNowPlaying()
}
}
}
@@ -443,6 +445,10 @@ class AudioPlayer: NSObject {
}
}
public func setChapterTrack() {
self.updateNowPlaying()
}
public func getCurrentTime() -> Double? {
guard let playbackSession = self.getPlaybackSession() else { return nil }
let currentTrackTime = self.audioPlayer.currentTime().seconds
@@ -657,7 +663,16 @@ class AudioPlayer: NSObject {
}
private func updateNowPlaying() {
NotificationCenter.default.post(name: NSNotification.Name(PlayerEvents.update.rawValue), object: nil)
if let duration = self.getDuration(), let currentTime = self.getCurrentTime() {
if let session = self.getPlaybackSession(), let currentChapter = session.getCurrentChapter(), PlayerSettings.main().chapterTrack {
NowPlayingInfo.shared.update(
duration: currentChapter.getRelativeChapterEndTime(),
currentTime: currentChapter.getRelativeChapterCurrentTime(sessionCurrentTime: session.currentTime),
rate: rate,
chapterName: currentChapter.title,
chapterNumber: (session.chapters.firstIndex(of: currentChapter) ?? 0) + 1,
chapterCount: session.chapters.count
)
} else if let duration = self.getDuration(), let currentTime = self.getCurrentTime() {
NowPlayingInfo.shared.update(duration: duration, currentTime: currentTime, rate: rate)
}
}
@@ -61,6 +61,10 @@ class PlayerHandler {
self.player?.setPlaybackRate(speed)
}
public static func setChapterTrack() {
self.player?.setChapterTrack()
}
public static func getSleepTimeRemaining() -> Double? {
return self.player?.getSleepTimeRemaining()
}