Skip to content

Commit a5ef88a

Browse files
authored
Merge pull request libre-tube#7044 from Bnyro/master
fix: watch position not set for downloaded videos
2 parents 15b380e + b9ff64d commit a5ef88a

3 files changed

Lines changed: 19 additions & 6 deletions

File tree

app/src/main/java/com/github/libretube/services/AbstractPlayerService.kt

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import android.os.Bundle
55
import android.os.Handler
66
import android.os.Looper
77
import android.view.KeyEvent
8+
import androidx.annotation.CallSuper
89
import androidx.annotation.OptIn
910
import androidx.core.app.ServiceCompat
1011
import androidx.core.os.bundleOf
@@ -51,7 +52,8 @@ abstract class AbstractPlayerService : MediaLibraryService(), MediaLibrarySessio
5152
lateinit var videoId: String
5253
private set
5354

54-
var isTransitioning = true
55+
var isTransitioning = false
56+
private set
5557

5658
val handler = Handler(Looper.getMainLooper())
5759

@@ -88,6 +90,14 @@ abstract class AbstractPlayerService : MediaLibraryService(), MediaLibrarySessio
8890
)
8991
}
9092
}
93+
94+
override fun onPlaybackStateChanged(playbackState: Int) {
95+
super.onPlaybackStateChanged(playbackState)
96+
97+
if (playbackState == Player.STATE_READY) {
98+
isTransitioning = false
99+
}
100+
}
91101
}
92102

93103
override fun onCustomCommand(
@@ -311,7 +321,10 @@ abstract class AbstractPlayerService : MediaLibraryService(), MediaLibrarySessio
311321
*
312322
* This function should base its actions on the videoId variable.
313323
*/
314-
abstract suspend fun startPlayback()
324+
@CallSuper
325+
open suspend fun startPlayback() {
326+
isTransitioning = true
327+
}
315328

316329
private fun saveWatchPosition() {
317330
if (isTransitioning || !watchPositionsEnabled) return

app/src/main/java/com/github/libretube/services/OfflinePlayerService.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ open class OfflinePlayerService : AbstractPlayerService() {
9090
* Attempt to start an audio player with the given download items
9191
*/
9292
override suspend fun startPlayback() {
93+
super.startPlayback()
94+
9395
val downloadWithItems = withContext(Dispatchers.IO) {
9496
Database.downloadDao().findById(videoId)
9597
}!!

app/src/main/java/com/github/libretube/services/OnlinePlayerService.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,6 @@ open class OnlinePlayerService : AbstractPlayerService() {
7777

7878
Player.STATE_BUFFERING -> {}
7979
Player.STATE_READY -> {
80-
isTransitioning = false
81-
8280
// save video to watch history when the video starts playing or is being resumed
8381
// waiting for the player to be ready since the video can't be claimed to be watched
8482
// while it did not yet start actually, but did buffer only so far
@@ -113,11 +111,11 @@ open class OnlinePlayerService : AbstractPlayerService() {
113111
}
114112

115113
override suspend fun startPlayback() {
114+
super.startPlayback()
115+
116116
val timestamp = startTimestamp ?: 0L
117117
startTimestamp = null
118118

119-
isTransitioning = true
120-
121119
streams = withContext(Dispatchers.IO) {
122120
try {
123121
StreamsExtractor.extractStreams(videoId)

0 commit comments

Comments
 (0)