Merge pull request #7044 from Bnyro/master

fix: watch position not set for downloaded videos
This commit is contained in:
Bnyro 2025-01-29 16:28:49 +01:00 committed by GitHub
commit a5ef88ad1a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 19 additions and 6 deletions

View File

@ -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
@ -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())
@ -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(
@ -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

View File

@ -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)
}!!

View File

@ -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
@ -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)