Skip to content

Commit

Permalink
Fix:Sleep timer independent from audio player time to be uneffected b…
Browse files Browse the repository at this point in the history
…y playback speed changes #86
  • Loading branch information
advplyr committed Feb 3, 2022
1 parent a30fe74 commit 6b59ad5
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ class MyNativeAudio : Plugin() {
emit("onSleepTimerEnded", currentPosition)
}

override fun onSleepTimerSet(sleepTimerEndTime: Long) {
emit("onSleepTimerSet", sleepTimerEndTime)
override fun onSleepTimerSet(sleepTimeRemaining: Int) {
emit("onSleepTimerSet", sleepTimeRemaining)
}
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class PlayerNotificationService : MediaBrowserServiceCompat() {
fun onMetadata(metadata: JSObject)
fun onPrepare(audiobookId: String, playWhenReady: Boolean)
fun onSleepTimerEnded(currentPosition: Long)
fun onSleepTimerSet(sleepTimerEndTime: Long)
fun onSleepTimerSet(sleepTimeRemaining: Int)
}

private val tag = "PlayerService"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.audiobookshelf.app

import android.hardware.SensorManager
import android.os.Handler
import android.os.Looper
import android.util.Log
Expand All @@ -17,6 +16,8 @@ class SleepTimerManager constructor(playerNotificationService:PlayerNotification
private var sleepTimerTask:TimerTask? = null
private var sleepTimerRunning:Boolean = false
private var sleepTimerEndTime:Long = 0L
private var sleepTimerLength:Long = 0L
private var sleepTimerElapsed:Long = 0L
private var sleepTimerExtensionTime:Long = 0L
private var sleepTimerFinishedAt:Long = 0L

Expand Down Expand Up @@ -45,20 +46,20 @@ class SleepTimerManager constructor(playerNotificationService:PlayerNotification
}

private fun getSleepTimerTimeRemainingSeconds():Int {
if (sleepTimerEndTime == 0L && sleepTimerLength > 0) { // For regular timer
return ((sleepTimerLength - sleepTimerElapsed) / 1000).toDouble().roundToInt()
}
// For chapter end timer
if (sleepTimerEndTime <= 0) return 0
var sleepTimeRemaining = sleepTimerEndTime - getCurrentTime()
return ((sleepTimeRemaining / 1000).toDouble()).roundToInt()
}

fun getIsSleepTimerRunning():Boolean {
return sleepTimerRunning
return (((sleepTimerEndTime - getCurrentTime()) / 1000).toDouble()).roundToInt()
}

fun setSleepTimer(time: Long, isChapterTime: Boolean) : Boolean {
Log.d(tag, "Setting Sleep Timer for $time is chapter time $isChapterTime")
sleepTimerTask?.cancel()
sleepTimerRunning = false
sleepTimerFinishedAt = 0L
sleepTimerElapsed = 0L

// Register shake sensor
playerNotificationService.registerSensor()
Expand All @@ -70,24 +71,36 @@ class SleepTimerManager constructor(playerNotificationService:PlayerNotification
return false
}
sleepTimerEndTime = time
sleepTimerLength = 0
sleepTimerExtensionTime = SLEEP_EXTENSION_TIME

if (sleepTimerEndTime > getDuration()) {
sleepTimerEndTime = getDuration()
}
} else {
sleepTimerEndTime = currentTime + time
sleepTimerLength = time
sleepTimerEndTime = 0L
sleepTimerExtensionTime = time
}

if (sleepTimerEndTime > getDuration()) {
sleepTimerEndTime = getDuration()
if (sleepTimerLength + getCurrentTime() > getDuration()) {
sleepTimerLength = getDuration() - getCurrentTime()
}
}

playerNotificationService.listener?.onSleepTimerSet(sleepTimerEndTime)
playerNotificationService.listener?.onSleepTimerSet(getSleepTimerTimeRemainingSeconds())

sleepTimerRunning = true
sleepTimerTask = Timer("SleepTimer", false).schedule(0L, 1000L) {
Handler(Looper.getMainLooper()).post() {
if (getIsPlaying()) {
sleepTimerElapsed += 1000L

var sleepTimeSecondsRemaining = getSleepTimerTimeRemainingSeconds()
Log.d(tag, "Sleep TIMER time remaining $sleepTimeSecondsRemaining s")
Log.d(tag, "Timer Elapsed $sleepTimerElapsed | Sleep TIMER time remaining $sleepTimeSecondsRemaining s")

if (sleepTimeSecondsRemaining > 0) {
playerNotificationService.listener?.onSleepTimerSet(sleepTimeSecondsRemaining)
}

if (sleepTimeSecondsRemaining <= 0) {
Log.d(tag, "Sleep Timer Pausing Player on Chapter")
Expand Down Expand Up @@ -129,9 +142,15 @@ class SleepTimerManager constructor(playerNotificationService:PlayerNotification
private fun extendSleepTime() {
if (!sleepTimerRunning) return
setVolume(1F)
sleepTimerEndTime += sleepTimerExtensionTime
if (sleepTimerEndTime > getDuration()) sleepTimerEndTime = getDuration()
playerNotificationService.listener?.onSleepTimerSet(sleepTimerEndTime)
if (sleepTimerEndTime == 0L) {
sleepTimerLength += sleepTimerExtensionTime
if (sleepTimerLength + getCurrentTime() > getDuration()) sleepTimerLength = getDuration() - getCurrentTime()
} else {
sleepTimerEndTime += sleepTimerExtensionTime
if (sleepTimerEndTime > getDuration()) sleepTimerEndTime = getDuration()
}

playerNotificationService.listener?.onSleepTimerSet(getSleepTimerTimeRemainingSeconds())
}

fun checkShouldExtendSleepTimer() {
Expand Down Expand Up @@ -164,27 +183,42 @@ class SleepTimerManager constructor(playerNotificationService:PlayerNotification
fun increaseSleepTime(time: Long) {
Log.d(tag, "Increase Sleep time $time")
if (!sleepTimerRunning) return
var newSleepEndTime = sleepTimerEndTime + time
sleepTimerEndTime = if (newSleepEndTime >= getDuration()) {
getDuration()

if (sleepTimerEndTime == 0L) {
sleepTimerLength += time
if (sleepTimerLength + getCurrentTime() > getDuration()) sleepTimerLength = getDuration() - getCurrentTime()
} else {
newSleepEndTime
var newSleepEndTime = sleepTimerEndTime + time
sleepTimerEndTime = if (newSleepEndTime >= getDuration()) {
getDuration()
} else {
newSleepEndTime
}
}

setVolume(1F)
playerNotificationService.listener?.onSleepTimerSet(sleepTimerEndTime)
playerNotificationService.listener?.onSleepTimerSet(getSleepTimerTimeRemainingSeconds())
}

fun decreaseSleepTime(time: Long) {
Log.d(tag, "Decrease Sleep time $time")
if (!sleepTimerRunning) return
var newSleepEndTime = sleepTimerEndTime - time
sleepTimerEndTime = if (newSleepEndTime <= 1000) {
// End sleep timer in 1 second
getCurrentTime() + 1000


if (sleepTimerEndTime == 0L) {
sleepTimerLength -= time
if (sleepTimerLength <= 0) sleepTimerLength = 1000L
} else {
newSleepEndTime
var newSleepEndTime = sleepTimerEndTime - time
sleepTimerEndTime = if (newSleepEndTime <= 1000) {
// End sleep timer in 1 second
getCurrentTime() + 1000
} else {
newSleepEndTime
}
}

setVolume(1F)
playerNotificationService.listener?.onSleepTimerSet(sleepTimerEndTime)
playerNotificationService.listener?.onSleepTimerSet(getSleepTimerTimeRemainingSeconds())
}
}
10 changes: 5 additions & 5 deletions components/app/AudioPlayer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export default {
},
loading: Boolean,
sleepTimerRunning: Boolean,
sleepTimerEndTime: Number
sleepTimeRemaining: Number
},
data() {
return {
Expand Down Expand Up @@ -241,10 +241,10 @@ export default {
if (!this.currentChapter) return 0
return this.currentChapter.end - this.currentTime
},
sleepTimeRemaining() {
if (!this.sleepTimerEndTime) return 0
return Math.max(0, this.sleepTimerEndTime / 1000 - this.currentTime)
},
// sleepTimeRemaining() {
// if (!this.sleepTimerEndTime) return 0
// return Math.max(0, this.sleepTimerEndTime / 1000 - this.currentTime)
// },
sleepTimeRemainingPretty() {
if (!this.sleepTimeRemaining) return '0s'
var secondsRemaining = Math.round(this.sleepTimeRemaining)
Expand Down
19 changes: 10 additions & 9 deletions components/app/AudioPlayerContainer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
:loading="isLoading"
:bookmarks="bookmarks"
:sleep-timer-running="isSleepTimerRunning"
:sleep-timer-end-time="sleepTimerEndTime"
:sleep-time-remaining="sleepTimeRemaining"
@close="cancelStream"
@sync="sync"
@setTotalDuration="setTotalDuration"
Expand Down Expand Up @@ -49,6 +49,7 @@ export default {
currentTime: 0,
isSleepTimerRunning: false,
sleepTimerEndTime: 0,
sleepTimerRemaining: 0,
onSleepTimerEndedListener: null,
onSleepTimerSetListener: null,
sleepInterval: null,
Expand Down Expand Up @@ -149,11 +150,11 @@ export default {
return `${this.$store.state.serverUrl}/s/book/${this.audiobook.id}/${trelpath}?token=${this.userToken}`
})
return tracks
},
sleepTimeRemaining() {
if (!this.sleepTimerEndTime) return 0
return Math.max(0, this.sleepTimerEndTime / 1000 - this.currentTime)
}
// sleepTimeRemaining() {
// if (!this.sleepTimerEndTime) return 0
// return Math.max(0, this.sleepTimerEndTime / 1000 - this.currentTime)
// }
},
methods: {
showBookmarks() {
Expand All @@ -175,16 +176,16 @@ export default {
this.updateTime(currentTime)
}
},
onSleepTimerSet({ value: sleepTimerEndTime }) {
console.log('SLEEP TIMER SET', sleepTimerEndTime)
if (sleepTimerEndTime === 0) {
onSleepTimerSet({ value: sleepTimeRemaining }) {
console.log('SLEEP TIMER SET', sleepTimeRemaining)
if (sleepTimeRemaining === 0) {
console.log('Sleep timer canceled')
this.isSleepTimerRunning = false
} else {
this.isSleepTimerRunning = true
}
this.sleepTimerEndTime = sleepTimerEndTime
this.sleepTimeRemaining = sleepTimeRemaining
},
showSleepTimer() {
if (this.currentChapter) {
Expand Down

0 comments on commit 6b59ad5

Please sign in to comment.