mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-14 14:20:30 +05:30
Merge pull request #3852 from Bnyro/master
Update watch positions every second to avoid outdated positions
This commit is contained in:
commit
dfc0ae449e
@ -71,6 +71,7 @@ import com.github.libretube.enums.PlayerEvent
|
|||||||
import com.github.libretube.enums.ShareObjectType
|
import com.github.libretube.enums.ShareObjectType
|
||||||
import com.github.libretube.extensions.formatShort
|
import com.github.libretube.extensions.formatShort
|
||||||
import com.github.libretube.extensions.hideKeyboard
|
import com.github.libretube.extensions.hideKeyboard
|
||||||
|
import com.github.libretube.extensions.setMetadata
|
||||||
import com.github.libretube.extensions.toID
|
import com.github.libretube.extensions.toID
|
||||||
import com.github.libretube.extensions.toastFromMainDispatcher
|
import com.github.libretube.extensions.toastFromMainDispatcher
|
||||||
import com.github.libretube.extensions.updateParameters
|
import com.github.libretube.extensions.updateParameters
|
||||||
@ -110,6 +111,9 @@ import com.github.libretube.util.PlayingQueue
|
|||||||
import com.github.libretube.util.TextUtils
|
import com.github.libretube.util.TextUtils
|
||||||
import com.github.libretube.util.TextUtils.toTimeInSeconds
|
import com.github.libretube.util.TextUtils.toTimeInSeconds
|
||||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||||
|
import java.io.IOException
|
||||||
|
import java.util.*
|
||||||
|
import java.util.concurrent.Executors
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
@ -117,12 +121,9 @@ import kotlinx.coroutines.runBlocking
|
|||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import kotlinx.serialization.encodeToString
|
import kotlinx.serialization.encodeToString
|
||||||
import retrofit2.HttpException
|
import retrofit2.HttpException
|
||||||
import java.io.IOException
|
|
||||||
import java.util.*
|
|
||||||
import java.util.concurrent.Executors
|
|
||||||
import com.github.libretube.extensions.setMetadata
|
|
||||||
import kotlin.math.abs
|
import kotlin.math.abs
|
||||||
|
|
||||||
|
|
||||||
@androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class)
|
@androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class)
|
||||||
class PlayerFragment : Fragment(), OnlinePlayerOptions {
|
class PlayerFragment : Fragment(), OnlinePlayerOptions {
|
||||||
private var _binding: FragmentPlayerBinding? = null
|
private var _binding: FragmentPlayerBinding? = null
|
||||||
@ -248,6 +249,14 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
|
|||||||
broadcastReceiver,
|
broadcastReceiver,
|
||||||
IntentFilter(PlayerHelper.getIntentActon(requireContext())),
|
IntentFilter(PlayerHelper.getIntentActon(requireContext())),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// schedule task to save the watch position each second
|
||||||
|
Timer().scheduleAtFixedRate(object : TimerTask() {
|
||||||
|
override fun run() {
|
||||||
|
handler.post(this@PlayerFragment::saveWatchPosition)
|
||||||
|
}
|
||||||
|
}, 1000, 1000)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onCreateView(
|
override fun onCreateView(
|
||||||
@ -616,7 +625,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (exoPlayer.duration != C.TIME_UNSET) saveWatchPosition()
|
saveWatchPosition()
|
||||||
|
|
||||||
PlayingQueue.clear()
|
PlayingQueue.clear()
|
||||||
|
|
||||||
@ -637,7 +646,12 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
|
|||||||
|
|
||||||
// save the watch position if video isn't finished and option enabled
|
// save the watch position if video isn't finished and option enabled
|
||||||
private fun saveWatchPosition() {
|
private fun saveWatchPosition() {
|
||||||
if (!PlayerHelper.watchPositionsVideo || exoPlayer.currentPosition == 0L) return
|
if (!this::exoPlayer.isInitialized || !PlayerHelper.watchPositionsVideo
|
||||||
|
|| exoPlayer.duration == C.TIME_UNSET || exoPlayer.currentPosition in listOf(
|
||||||
|
0L,
|
||||||
|
C.TIME_UNSET
|
||||||
|
)
|
||||||
|
) return
|
||||||
val watchPosition = WatchPosition(videoId!!, exoPlayer.currentPosition)
|
val watchPosition = WatchPosition(videoId!!, exoPlayer.currentPosition)
|
||||||
CoroutineScope(Dispatchers.IO).launch {
|
CoroutineScope(Dispatchers.IO).launch {
|
||||||
Database.watchPositionDao().insert(watchPosition)
|
Database.watchPositionDao().insert(watchPosition)
|
||||||
@ -949,11 +963,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onPlaybackStateChanged(playbackState: Int) {
|
override fun onPlaybackStateChanged(playbackState: Int) {
|
||||||
// save the watch position to the database
|
saveWatchPosition()
|
||||||
// only called when the position is unequal to 0, otherwise it would become reset
|
|
||||||
// before the player can seek to the saved position from videos of the queue
|
|
||||||
// not called when the video has ended, since it then might save it to the next autoplay video
|
|
||||||
if (playbackState != Player.STATE_ENDED) saveWatchPosition()
|
|
||||||
|
|
||||||
// check if video has ended, next video is available and autoplay is enabled.
|
// check if video has ended, next video is available and autoplay is enabled.
|
||||||
if (
|
if (
|
||||||
@ -1567,7 +1577,9 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun shouldStartPiP(): Boolean {
|
private fun shouldStartPiP(): Boolean {
|
||||||
return usePiP() && exoPlayer.isPlaying && !BackgroundHelper.isBackgroundServiceRunning(requireContext())
|
return usePiP() && exoPlayer.isPlaying && !BackgroundHelper.isBackgroundServiceRunning(
|
||||||
|
requireContext()
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun killPlayerFragment() {
|
private fun killPlayerFragment() {
|
||||||
|
Loading…
Reference in New Issue
Block a user