Skip to content

Commit 22894a3

Browse files
Fix skip back behaviour when jumpin to start of book (#201)
After last changes, pressing the jump back button when less than 30 seconds of the book had been played caused the pointer to jump to the beginning, but not the audiobook itself. This was due to the AudioBookPlayer not jumping back if there was no actual point to jump to. This is now fixed by computing how much of the book we have listened, and if it's less than 30 seconds, we jump to the beginning of the book. Speeding up the book causes the jump to beginning happen seemingly from further away than 30 seconds, but only a few seconds further away. It shouldn't be too noticeable for users. NOTE: If the jump to the beginning happens from another chapter than the first one, the audiobook pauses.
1 parent 886cd3a commit 22894a3

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

simplified-viewer-audiobook/src/main/java/org/librarysimplified/viewer/audiobook/EkirjaPlayerFragment.kt

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,12 @@ class EkirjaPlayerFragment : Fragment(), AudioManager.OnAudioFocusChangeListener
597597
*/
598598
private fun skipBack() {
599599
updatePlayerPosition = true
600+
//Skipping back doesn't work correctly if the book hasn't been played for 30 seconds
601+
//So in that special case, we skip to te beginning of the book
602+
// If we jump from another chapter to the beginning, the player pauses
603+
if(needsToSkipToBeginning()) {
604+
return player.playAtBookStart()
605+
}
600606
return player.skipBack()
601607
}
602608

@@ -1139,7 +1145,7 @@ class EkirjaPlayerFragment : Fragment(), AudioManager.OnAudioFocusChangeListener
11391145

11401146
//Current time is accurate presentation of the time
11411147
//That current chapter has played
1142-
val currTime = TimeUnit.SECONDS.toMillis(lastStartedPlayerPosition
1148+
val currentTime = TimeUnit.SECONDS.toMillis(lastStartedPlayerPosition
11431149
.plus(
11441150
(TimeUnit.MILLISECONDS.toSeconds(
11451151
offsetMilliseconds
@@ -1151,19 +1157,19 @@ class EkirjaPlayerFragment : Fragment(), AudioManager.OnAudioFocusChangeListener
11511157
playerRemainingBookTime.text =
11521158
PlayerTimeStrings.hourMinuteTextFromRemainingTime(
11531159
requireContext(),
1154-
(getCurrentAudiobookRemainingDuration(spineElement) - currTime)
1160+
(getCurrentAudiobookRemainingDuration(spineElement) - currentTime)
11551161
)
11561162

11571163
// The visual presentation of how much of the chapter is left
11581164
this.playerTimeMaximum.text =
11591165
PlayerTimeStrings.hourMinuteSecondTextFromDurationOptional(spineElement.duration
1160-
?.minus(currTime))
1166+
?.minus(currentTime))
11611167
this.playerTimeMaximum.contentDescription =
11621168
this.playerTimeRemainingSpokenOptional(offsetMilliseconds, spineElement.duration)
11631169

11641170
// The visual presentation of how much of the chapter is done
11651171
this.playerTimeCurrent.text =
1166-
PlayerTimeStrings.hourMinuteSecondTextFromMilliseconds(currTime)
1172+
PlayerTimeStrings.hourMinuteSecondTextFromMilliseconds(currentTime)
11671173
this.playerTimeCurrent.contentDescription =
11681174
this.playerTimeCurrentSpoken(offsetMilliseconds)
11691175

@@ -1339,4 +1345,28 @@ class EkirjaPlayerFragment : Fragment(), AudioManager.OnAudioFocusChangeListener
13391345

13401346
playerService.updatePlayerInfo(this.playerInfoModel)
13411347
}
1348+
1349+
/**
1350+
* Returns true if we have played the book for less than 30 seconds
1351+
*/
1352+
private fun needsToSkipToBeginning() : Boolean {
1353+
//Length of whole book
1354+
val wholeBook = book.spine.sumOf { it.duration?.millis ?: 0L }
1355+
//How much of current chapter has passed
1356+
val currentChapterPassed = TimeUnit.SECONDS.toMillis(lastStartedPlayerPosition
1357+
.plus(
1358+
(TimeUnit.MILLISECONDS.toSeconds(
1359+
this.playerPositionCurrentOffset
1360+
).minus(lastStartedPlayerPosition
1361+
))))
1362+
1363+
//How much of book is remaining
1364+
val bookRemaining = getCurrentAudiobookRemainingDuration(this.playerPositionCurrentSpine!!).minus(currentChapterPassed)
1365+
1366+
// Return true if the difference is less than 30 seconds,
1367+
// which indicates the need to jump to the beginning of the book
1368+
// NOTE: On faster play rates, the jump to start of book happens also from a little further
1369+
// due to the increased speed
1370+
return wholeBook.minus(bookRemaining) < 30000
1371+
}
13421372
}

0 commit comments

Comments
 (0)