Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: watch position not set for downloaded videos #7044

Merged
merged 1 commit into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading