mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2025-08-29 06:18:51 +02:00
Fix edge case when seeking past chapter
This commit is contained in:
parent
eb7a241e94
commit
2076b93e19
2 changed files with 31 additions and 7 deletions
|
@ -423,8 +423,13 @@ class AudioPlayer: NSObject {
|
||||||
|
|
||||||
private func rescheduleSleepTimerAtTime(time: Double, secondsRemaining: Int?) {
|
private func rescheduleSleepTimerAtTime(time: Double, secondsRemaining: Int?) {
|
||||||
// Not a chapter sleep timer
|
// Not a chapter sleep timer
|
||||||
|
let hadToCancelChapterSleepTimer = decideIfChapterSleepTimerNeedsToBeCanceled(time: time)
|
||||||
|
guard !hadToCancelChapterSleepTimer else { return }
|
||||||
guard PlayerHandler.sleepTimerChapterStopTime == nil else { return }
|
guard PlayerHandler.sleepTimerChapterStopTime == nil else { return }
|
||||||
|
|
||||||
|
// Verify sleep timer is set
|
||||||
|
guard self.sleepTimeToken != nil else { return }
|
||||||
|
|
||||||
// Update the sleep timer
|
// Update the sleep timer
|
||||||
if let secondsRemaining = secondsRemaining {
|
if let secondsRemaining = secondsRemaining {
|
||||||
let newSleepTimerPosition = time + Double(secondsRemaining)
|
let newSleepTimerPosition = time + Double(secondsRemaining)
|
||||||
|
@ -432,6 +437,19 @@ class AudioPlayer: NSObject {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private func decideIfChapterSleepTimerNeedsToBeCanceled(time: Double) -> Bool {
|
||||||
|
if let chapterSleepTime = PlayerHandler.sleepTimerChapterStopTime {
|
||||||
|
let sleepIsBeforeCurrentTime = Double(chapterSleepTime) <= time
|
||||||
|
if sleepIsBeforeCurrentTime {
|
||||||
|
PlayerHandler.sleepTimerChapterStopTime = nil
|
||||||
|
self.removeSleepTimer()
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
public func increaseSleepTime(extraTimeInSeconds: Double) {
|
public func increaseSleepTime(extraTimeInSeconds: Double) {
|
||||||
if let sleepTime = PlayerHandler.remainingSleepTime {
|
if let sleepTime = PlayerHandler.remainingSleepTime {
|
||||||
let currentTime = getCurrentTime()
|
let currentTime = getCurrentTime()
|
||||||
|
|
|
@ -63,16 +63,22 @@ class PlayerHandler {
|
||||||
get {
|
get {
|
||||||
guard let player = player else { return nil }
|
guard let player = player else { return nil }
|
||||||
|
|
||||||
|
// Return the player time until sleep
|
||||||
|
var timeUntilSleep: Double? = nil
|
||||||
|
if let sleepTimerChapterStopTime = sleepTimerChapterStopTime {
|
||||||
|
timeUntilSleep = Double(sleepTimerChapterStopTime) - player.getCurrentTime()
|
||||||
|
} else if let stopAt = player.getSleepStopAt() {
|
||||||
|
timeUntilSleep = stopAt - player.getCurrentTime()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Scale the time until sleep based on the playback rate
|
||||||
|
if let timeUntilSleep = timeUntilSleep {
|
||||||
// Consider paused as playing at 1x
|
// Consider paused as playing at 1x
|
||||||
let rate = Double(player.rate > 0 ? player.rate : 1)
|
let rate = Double(player.rate > 0 ? player.rate : 1)
|
||||||
|
|
||||||
if let sleepTimerChapterStopTime = sleepTimerChapterStopTime {
|
|
||||||
let timeUntilChapterEnd = Double(sleepTimerChapterStopTime) - player.getCurrentTime()
|
|
||||||
let timeUntilChapterEndScaled = timeUntilChapterEnd / rate
|
|
||||||
return Int(timeUntilChapterEndScaled.rounded())
|
|
||||||
} else if let stopAt = player.getSleepStopAt() {
|
|
||||||
let timeUntilSleep = stopAt - player.getCurrentTime()
|
|
||||||
let timeUntilSleepScaled = timeUntilSleep / rate
|
let timeUntilSleepScaled = timeUntilSleep / rate
|
||||||
|
guard timeUntilSleepScaled.isNaN == false else { return nil }
|
||||||
|
|
||||||
return Int(timeUntilSleepScaled.rounded())
|
return Int(timeUntilSleepScaled.rounded())
|
||||||
} else {
|
} else {
|
||||||
return nil
|
return nil
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue