From 3840f2f344b0e4260c31477d69dbb6cd45ff1cf7 Mon Sep 17 00:00:00 2001 From: advplyr Date: Tue, 21 Jun 2022 18:22:47 -0500 Subject: [PATCH] Update:Less aggressive seek back after pausing, first seek back starts on a 10s pause and jumps back 3s #230. Minimum seek back after a pause is the current chapter start time #244 --- .../com/audiobookshelf/app/data/DataClasses.kt | 7 ++++++- .../audiobookshelf/app/data/PlaybackSession.kt | 10 ++++++++-- .../audiobookshelf/app/player/PlayerListener.kt | 17 ++++++++++++----- .../app/player/PlayerNotificationService.kt | 4 ++++ 4 files changed, 30 insertions(+), 8 deletions(-) diff --git a/android/app/src/main/java/com/audiobookshelf/app/data/DataClasses.kt b/android/app/src/main/java/com/audiobookshelf/app/data/DataClasses.kt index 4f97b373..0e926874 100644 --- a/android/app/src/main/java/com/audiobookshelf/app/data/DataClasses.kt +++ b/android/app/src/main/java/com/audiobookshelf/app/data/DataClasses.kt @@ -399,7 +399,12 @@ data class BookChapter( var start:Double, var end:Double, var title:String? -) +) { + @get:JsonIgnore + val startMs get() = (start * 1000L).toLong() + @get:JsonIgnore + val endMs get() = (end * 1000L).toLong() +} @JsonIgnoreProperties(ignoreUnknown = true) data class MediaProgress( diff --git a/android/app/src/main/java/com/audiobookshelf/app/data/PlaybackSession.kt b/android/app/src/main/java/com/audiobookshelf/app/data/PlaybackSession.kt index 6af1c70c..eb38c9d1 100644 --- a/android/app/src/main/java/com/audiobookshelf/app/data/PlaybackSession.kt +++ b/android/app/src/main/java/com/audiobookshelf/app/data/PlaybackSession.kt @@ -67,15 +67,21 @@ class PlaybackSession( @JsonIgnore fun getCurrentTrackIndex():Int { - for (i in 0..(audioTracks.size - 1)) { + for (i in 0 until audioTracks.size) { val track = audioTracks[i] - if (currentTimeMs >= track.startOffsetMs && (track.endOffsetMs) > currentTimeMs) { + if (currentTimeMs >= track.startOffsetMs && (track.endOffsetMs > currentTimeMs)) { return i } } return audioTracks.size - 1 } + @JsonIgnore + fun getChapterForTime(time:Long):BookChapter? { + if (chapters.isEmpty()) return null + return chapters.find { time >= it.startMs && it.endMs > time} + } + @JsonIgnore fun getCurrentTrackTimeMs():Long { val currentTrack = audioTracks[this.getCurrentTrackIndex()] diff --git a/android/app/src/main/java/com/audiobookshelf/app/player/PlayerListener.kt b/android/app/src/main/java/com/audiobookshelf/app/player/PlayerListener.kt index a3d15c0b..9c521ab2 100644 --- a/android/app/src/main/java/com/audiobookshelf/app/player/PlayerListener.kt +++ b/android/app/src/main/java/com/audiobookshelf/app/player/PlayerListener.kt @@ -75,12 +75,18 @@ class PlayerListener(var playerNotificationService:PlayerNotificationService) : Log.d(tag, "SeekBackTime: playing started now set seek back time $lastPauseTime") var backTime = calcPauseSeekBackTime() if (backTime > 0) { - if (backTime >= playerNotificationService.getCurrentTime()) backTime = playerNotificationService.getCurrentTime() - 500 + // Current chapter is used so that seek back does not go back to the previous chapter + val currentChapter = playerNotificationService.getCurrentBookChapter() + val minSeekBackTime = currentChapter?.startMs ?: 0 + + val currentTime = playerNotificationService.getCurrentTime() + val newTime = currentTime - backTime + if (newTime < minSeekBackTime) { + backTime = currentTime - minSeekBackTime + } Log.d(tag, "SeekBackTime $backTime") onSeekBack = true playerNotificationService.seekBackward(backTime) - } else { - Log.d(tag, "SeekBackTime: back time is 0") } } @@ -113,8 +119,9 @@ class PlayerListener(var playerNotificationService:PlayerNotificationService) : if (lastPauseTime <= 0) return 0 val time: Long = System.currentTimeMillis() - lastPauseTime val seekback: Long - if (time < 3000) seekback = 0 - else if (time < 300000) seekback = 10000 // 3s to 5m = jump back 10s + if (time < 10000) seekback = 0 // 10s or less = no seekback + else if (time < 60000) seekback = 3000 // 10s to 1m = jump back 3s + else if (time < 300000) seekback = 10000 // 1m to 5m = jump back 10s else if (time < 1800000) seekback = 20000 // 5m to 30m = jump back 20s else seekback = 29500 // 30m and up = jump back 30s return seekback diff --git a/android/app/src/main/java/com/audiobookshelf/app/player/PlayerNotificationService.kt b/android/app/src/main/java/com/audiobookshelf/app/player/PlayerNotificationService.kt index 6a50bdab..27478cc7 100644 --- a/android/app/src/main/java/com/audiobookshelf/app/player/PlayerNotificationService.kt +++ b/android/app/src/main/java/com/audiobookshelf/app/player/PlayerNotificationService.kt @@ -507,6 +507,10 @@ class PlayerNotificationService : MediaBrowserServiceCompat() { return currentPlaybackSession?.id } + fun getCurrentBookChapter():BookChapter? { + return currentPlaybackSession?.getChapterForTime(this.getCurrentTime()) + } + // Called from PlayerListener play event // check with server if progress has updated since last play and sync progress update fun checkCurrentSessionProgress():Boolean {