Skip to content

Commit

Permalink
Merge pull request #7044 from Bnyro/master
Browse files Browse the repository at this point in the history
fix: watch position not set for downloaded videos
  • Loading branch information
Bnyro authored Jan 29, 2025
2 parents 15b380e + b9ff64d commit a5ef88a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.KeyEvent
import androidx.annotation.CallSuper
import androidx.annotation.OptIn
import androidx.core.app.ServiceCompat
import androidx.core.os.bundleOf
Expand Down Expand Up @@ -51,7 +52,8 @@ abstract class AbstractPlayerService : MediaLibraryService(), MediaLibrarySessio
lateinit var videoId: String
private set

var isTransitioning = true
var isTransitioning = false
private set

val handler = Handler(Looper.getMainLooper())

Expand Down Expand Up @@ -88,6 +90,14 @@ abstract class AbstractPlayerService : MediaLibraryService(), MediaLibrarySessio
)
}
}

override fun onPlaybackStateChanged(playbackState: Int) {
super.onPlaybackStateChanged(playbackState)

if (playbackState == Player.STATE_READY) {
isTransitioning = false
}
}
}

override fun onCustomCommand(
Expand Down Expand Up @@ -311,7 +321,10 @@ abstract class AbstractPlayerService : MediaLibraryService(), MediaLibrarySessio
*
* This function should base its actions on the videoId variable.
*/
abstract suspend fun startPlayback()
@CallSuper
open suspend fun startPlayback() {
isTransitioning = true
}

private fun saveWatchPosition() {
if (isTransitioning || !watchPositionsEnabled) return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ open class OfflinePlayerService : AbstractPlayerService() {
* Attempt to start an audio player with the given download items
*/
override suspend fun startPlayback() {
super.startPlayback()

val downloadWithItems = withContext(Dispatchers.IO) {
Database.downloadDao().findById(videoId)
}!!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ open class OnlinePlayerService : AbstractPlayerService() {

Player.STATE_BUFFERING -> {}
Player.STATE_READY -> {
isTransitioning = false

// save video to watch history when the video starts playing or is being resumed
// waiting for the player to be ready since the video can't be claimed to be watched
// while it did not yet start actually, but did buffer only so far
Expand Down Expand Up @@ -113,11 +111,11 @@ open class OnlinePlayerService : AbstractPlayerService() {
}

override suspend fun startPlayback() {
super.startPlayback()

val timestamp = startTimestamp ?: 0L
startTimestamp = null

isTransitioning = true

streams = withContext(Dispatchers.IO) {
try {
StreamsExtractor.extractStreams(videoId)
Expand Down

0 comments on commit a5ef88a

Please sign in to comment.