mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2025-08-30 22:59:35 +02:00
Fix auto rewind when check resetting sleep timer
This commit is contained in:
parent
daff8355d7
commit
8061ac0ced
3 changed files with 41 additions and 16 deletions
|
@ -23,6 +23,7 @@ class SleepTimerManager constructor(private val playerNotificationService: Playe
|
||||||
private var sleepTimerFinishedAt:Long = 0L
|
private var sleepTimerFinishedAt:Long = 0L
|
||||||
private var isAutoSleepTimer:Boolean = false // When timer was auto-set
|
private var isAutoSleepTimer:Boolean = false // When timer was auto-set
|
||||||
private var isFirstAutoSleepTimer: Boolean = true
|
private var isFirstAutoSleepTimer: Boolean = true
|
||||||
|
private var sleepTimerSessionId:String = ""
|
||||||
|
|
||||||
private fun getCurrentTime():Long {
|
private fun getCurrentTime():Long {
|
||||||
return playerNotificationService.getCurrentTime()
|
return playerNotificationService.getCurrentTime()
|
||||||
|
@ -125,7 +126,8 @@ class SleepTimerManager constructor(private val playerNotificationService: Playe
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setManualSleepTimer(time: Long, isChapterTime:Boolean):Boolean {
|
fun setManualSleepTimer(playbackSessionId:String, time: Long, isChapterTime:Boolean):Boolean {
|
||||||
|
sleepTimerSessionId = playbackSessionId
|
||||||
isAutoSleepTimer = false
|
isAutoSleepTimer = false
|
||||||
return setSleepTimer(time, isChapterTime)
|
return setSleepTimer(time, isChapterTime)
|
||||||
}
|
}
|
||||||
|
@ -135,7 +137,6 @@ class SleepTimerManager constructor(private val playerNotificationService: Playe
|
||||||
sleepTimerTask = null
|
sleepTimerTask = null
|
||||||
sleepTimerEndTime = 0
|
sleepTimerEndTime = 0
|
||||||
sleepTimerRunning = false
|
sleepTimerRunning = false
|
||||||
isAutoSleepTimer = false
|
|
||||||
playerNotificationService.unregisterSensor()
|
playerNotificationService.unregisterSensor()
|
||||||
|
|
||||||
setVolume(1f)
|
setVolume(1f)
|
||||||
|
@ -229,6 +230,17 @@ class SleepTimerManager constructor(private val playerNotificationService: Playe
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Automatically Rewind in the book if settings is enabled
|
||||||
|
if (isAutoSleepTimer) {
|
||||||
|
DeviceManager.deviceData.deviceSettings?.let { deviceSettings ->
|
||||||
|
if (deviceSettings.autoSleepTimerAutoRewind && !isFirstAutoSleepTimer) {
|
||||||
|
Log.i(tag, "Auto sleep timer auto rewind seeking back ${deviceSettings.autoSleepTimerAutoRewindTime}ms")
|
||||||
|
playerNotificationService.seekBackward(deviceSettings.autoSleepTimerAutoRewindTime)
|
||||||
|
}
|
||||||
|
isFirstAutoSleepTimer = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Set sleep timer
|
// Set sleep timer
|
||||||
// When sleepTimerLength is 0 then use end of chapter/track time
|
// When sleepTimerLength is 0 then use end of chapter/track time
|
||||||
if (sleepTimerLength == 0L) {
|
if (sleepTimerLength == 0L) {
|
||||||
|
@ -346,11 +358,12 @@ class SleepTimerManager constructor(private val playerNotificationService: Playe
|
||||||
Log.i(tag, "Current hour $currentHour is between ${deviceSettings.autoSleepTimerStartTime} and ${deviceSettings.autoSleepTimerEndTime} - starting sleep timer")
|
Log.i(tag, "Current hour $currentHour is between ${deviceSettings.autoSleepTimerStartTime} and ${deviceSettings.autoSleepTimerEndTime} - starting sleep timer")
|
||||||
|
|
||||||
// Automatically Rewind in the book if settings is enabled
|
// Automatically Rewind in the book if settings is enabled
|
||||||
if (deviceSettings.autoSleepTimerAutoRewind and !isFirstAutoSleepTimer) {
|
if (deviceSettings.autoSleepTimerAutoRewind && !isFirstAutoSleepTimer) {
|
||||||
|
Log.i(tag, "Auto sleep timer auto rewind seeking back ${deviceSettings.autoSleepTimerAutoRewindTime}ms")
|
||||||
playerNotificationService.seekBackward(deviceSettings.autoSleepTimerAutoRewindTime)
|
playerNotificationService.seekBackward(deviceSettings.autoSleepTimerAutoRewindTime)
|
||||||
} else {
|
|
||||||
isFirstAutoSleepTimer = false
|
|
||||||
}
|
}
|
||||||
|
isFirstAutoSleepTimer = false
|
||||||
|
|
||||||
// Set sleep timer
|
// Set sleep timer
|
||||||
// When sleepTimerLength is 0 then use end of chapter/track time
|
// When sleepTimerLength is 0 then use end of chapter/track time
|
||||||
if (deviceSettings.sleepTimerLength == 0L) {
|
if (deviceSettings.sleepTimerLength == 0L) {
|
||||||
|
@ -366,16 +379,24 @@ class SleepTimerManager constructor(private val playerNotificationService: Playe
|
||||||
setSleepTimer(deviceSettings.sleepTimerLength, false)
|
setSleepTimer(deviceSettings.sleepTimerLength, false)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (!isFirstAutoSleepTimer) {
|
Log.d(tag, "[TEST] sleep timer window not in $isFirstAutoSleepTimer")
|
||||||
isFirstAutoSleepTimer = true
|
isFirstAutoSleepTimer = true
|
||||||
}
|
|
||||||
Log.d(tag, "Current hour $currentHour is NOT between ${deviceSettings.autoSleepTimerStartTime} and ${deviceSettings.autoSleepTimerEndTime}")
|
Log.d(tag, "Current hour $currentHour is NOT between ${deviceSettings.autoSleepTimerStartTime} and ${deviceSettings.autoSleepTimerEndTime}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun handleMediaPlayEvent() {
|
fun handleMediaPlayEvent(playbackSessionId:String) {
|
||||||
checkShouldResetSleepTimer()
|
// Check if the playback session has changed
|
||||||
|
// If it hasn't changed OR the sleep timer is running then check reset the timer
|
||||||
|
// e.g. You set a manual sleep timer for 10 mins, then decide to change books, the sleep timer will stay on and reset to 10 mins
|
||||||
|
if (sleepTimerSessionId == playbackSessionId || sleepTimerRunning) {
|
||||||
|
checkShouldResetSleepTimer()
|
||||||
|
} else {
|
||||||
|
isFirstAutoSleepTimer = true
|
||||||
|
sleepTimerSessionId = playbackSessionId
|
||||||
|
}
|
||||||
|
|
||||||
checkAutoSleepTimer()
|
checkAutoSleepTimer()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -92,12 +92,15 @@ class PlayerListener(var playerNotificationService:PlayerNotificationService) :
|
||||||
|
|
||||||
// Start/stop progress sync interval
|
// Start/stop progress sync interval
|
||||||
if (isPlaying) {
|
if (isPlaying) {
|
||||||
// Handles auto-starting sleep timer and resetting sleep timer
|
|
||||||
playerNotificationService.sleepTimerManager.handleMediaPlayEvent()
|
|
||||||
|
|
||||||
player.volume = 1F // Volume on sleep timer might have decreased this
|
|
||||||
val playbackSession: PlaybackSession? = playerNotificationService.mediaProgressSyncer.currentPlaybackSession ?: playerNotificationService.currentPlaybackSession
|
val playbackSession: PlaybackSession? = playerNotificationService.mediaProgressSyncer.currentPlaybackSession ?: playerNotificationService.currentPlaybackSession
|
||||||
playbackSession?.let { playerNotificationService.mediaProgressSyncer.play(it) }
|
playbackSession?.let {
|
||||||
|
// Handles auto-starting sleep timer and resetting sleep timer
|
||||||
|
playerNotificationService.sleepTimerManager.handleMediaPlayEvent(it.id)
|
||||||
|
|
||||||
|
player.volume = 1F // Volume on sleep timer might have decreased this
|
||||||
|
|
||||||
|
playerNotificationService.mediaProgressSyncer.play(it)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
playerNotificationService.mediaProgressSyncer.pause {
|
playerNotificationService.mediaProgressSyncer.pause {
|
||||||
Log.d(tag, "Media Progress Syncer paused and synced")
|
Log.d(tag, "Media Progress Syncer paused and synced")
|
||||||
|
|
|
@ -341,7 +341,8 @@ class AbsAudioPlayer : Plugin() {
|
||||||
val isChapterTime:Boolean = call.getBoolean("isChapterTime", false) == true
|
val isChapterTime:Boolean = call.getBoolean("isChapterTime", false) == true
|
||||||
|
|
||||||
Handler(Looper.getMainLooper()).post {
|
Handler(Looper.getMainLooper()).post {
|
||||||
val success:Boolean = playerNotificationService.sleepTimerManager.setManualSleepTimer(time, isChapterTime)
|
val playbackSession: PlaybackSession? = playerNotificationService.mediaProgressSyncer.currentPlaybackSession ?: playerNotificationService.currentPlaybackSession
|
||||||
|
val success:Boolean = playerNotificationService.sleepTimerManager.setManualSleepTimer(playbackSession?.id ?: "", time, isChapterTime)
|
||||||
val ret = JSObject()
|
val ret = JSObject()
|
||||||
ret.put("success", success)
|
ret.put("success", success)
|
||||||
call.resolve(ret)
|
call.resolve(ret)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue