From 3eabb537f78fe250be0ffbc05d39503352e4e7df Mon Sep 17 00:00:00 2001 From: Christian Schabesberger Date: Wed, 4 Sep 2024 15:13:21 +0200 Subject: [PATCH] update playlist playtime --- .../newplayer/model/VideoPlayerUIState.kt | 3 ++ .../model/VideoPlayerViewModelImpl.kt | 35 ++++++++++++++++--- .../streamselect/StreamSelectTopBar.kt | 5 ++- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/new-player/src/main/java/net/newpipe/newplayer/model/VideoPlayerUIState.kt b/new-player/src/main/java/net/newpipe/newplayer/model/VideoPlayerUIState.kt index 02b0221..fd8c842 100644 --- a/new-player/src/main/java/net/newpipe/newplayer/model/VideoPlayerUIState.kt +++ b/new-player/src/main/java/net/newpipe/newplayer/model/VideoPlayerUIState.kt @@ -36,6 +36,7 @@ data class VideoPlayerUIState( val isLoading: Boolean, val durationInMs: Long, val playbackPositionInMs: Long, + val playbackPositionInPlaylistS: Int, val fastSeekSeconds: Int, val soundVolume: Float, val brightness: Float?, // when null use system value @@ -58,6 +59,7 @@ data class VideoPlayerUIState( bufferedPercentage = 0f, isLoading = true, durationInMs = 0, + playbackPositionInPlaylistS = 0, playbackPositionInMs = 0, fastSeekSeconds = 0, soundVolume = 0f, @@ -78,6 +80,7 @@ data class VideoPlayerUIState( bufferedPercentage = 0.5f, isLoading = false, durationInMs = 420, + playbackPositionInPlaylistS = 5039, playbackPositionInMs = 69, fastSeekSeconds = 10, soundVolume = 0.5f, diff --git a/new-player/src/main/java/net/newpipe/newplayer/model/VideoPlayerViewModelImpl.kt b/new-player/src/main/java/net/newpipe/newplayer/model/VideoPlayerViewModelImpl.kt index 387455c..6cccebd 100644 --- a/new-player/src/main/java/net/newpipe/newplayer/model/VideoPlayerViewModelImpl.kt +++ b/new-player/src/main/java/net/newpipe/newplayer/model/VideoPlayerViewModelImpl.kt @@ -30,8 +30,6 @@ import androidx.core.content.ContextCompat.getSystemService import androidx.lifecycle.AndroidViewModel import androidx.lifecycle.SavedStateHandle import androidx.lifecycle.viewModelScope -import androidx.media3.common.MediaItem -import androidx.media3.common.MediaMetadata import androidx.media3.common.Player import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.Job @@ -42,7 +40,6 @@ import kotlinx.coroutines.flow.SharedFlow import kotlinx.coroutines.flow.asSharedFlow import javax.inject.Inject import kotlinx.coroutines.flow.asStateFlow -import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch import net.newpipe.newplayer.Chapter @@ -73,6 +70,7 @@ class VideoPlayerViewModelImpl @Inject constructor( private var uiVisibilityJob: Job? = null private var progressUpdaterJob: Job? = null + private var playlistProgressUpdatrJob: Job? = null // this is necesary to restore the embedded view UI configuration when returning from fullscreen private var embeddedUiConfig: EmbeddedUiConfig? = null @@ -295,7 +293,6 @@ class VideoPlayerViewModelImpl @Inject constructor( delay(1000) } } - } private fun updateProgressOnce() { @@ -310,7 +307,33 @@ class VideoPlayerViewModelImpl @Inject constructor( seekerPosition = progressPercentage, durationInMs = duration, playbackPositionInMs = progress, - bufferedPercentage = bufferedPercentage + bufferedPercentage = bufferedPercentage, + ) + } + } + + private fun resetPlaylistProgressUpdaterJob() { + playlistProgressUpdatrJob?.cancel() + playlistProgressUpdatrJob = viewModelScope.launch { + while (true) { + updateProgressInPlaylistOnce() + delay(1000) + } + } + } + + private fun updateProgressInPlaylistOnce() { + var progress = 0 + val currentlyPlaying = uiState.value.currentlyPlaying.uniqueId + for (item in uiState.value.playList) { + if (item.uniqueId == currentlyPlaying) + break; + progress += item.lengthInS + } + progress += ((newPlayer?.currentPosition ?: 0) / 1000).toInt() + mutableUiState.update { + it.copy( + playbackPositionInPlaylistS = progress ) } } @@ -396,6 +419,7 @@ class VideoPlayerViewModelImpl @Inject constructor( } override fun openStreamSelection(selectChapter: Boolean, embeddedUiConfig: EmbeddedUiConfig) { + resetPlaylistProgressUpdaterJob() uiVisibilityJob?.cancel() if (!uiState.value.uiMode.fullscreen) { this.embeddedUiConfig = embeddedUiConfig @@ -407,6 +431,7 @@ class VideoPlayerViewModelImpl @Inject constructor( } override fun closeStreamSelection() { + playlistProgressUpdatrJob?.cancel() updateUiMode(uiState.value.uiMode.getUiHiddenState()) } diff --git a/new-player/src/main/java/net/newpipe/newplayer/ui/videoplayer/streamselect/StreamSelectTopBar.kt b/new-player/src/main/java/net/newpipe/newplayer/ui/videoplayer/streamselect/StreamSelectTopBar.kt index f7da6ea..8ca33c9 100644 --- a/new-player/src/main/java/net/newpipe/newplayer/ui/videoplayer/streamselect/StreamSelectTopBar.kt +++ b/new-player/src/main/java/net/newpipe/newplayer/ui/videoplayer/streamselect/StreamSelectTopBar.kt @@ -69,8 +69,11 @@ fun StreamSelectTopBar( val locale = getLocale()!! val duration = getPlaylistDurationInS(uiState.playList).toLong() * 1000 val durationString = getTimeStringFromMs(timeSpanInMs = duration, locale) + val playbackPositionString = getTimeStringFromMs( + timeSpanInMs = uiState.playbackPositionInPlaylistS.toLong() * 1000, locale = locale + ) Text( - text = "00:00/$durationString", + text = "$playbackPositionString/$durationString", maxLines = 1, overflow = TextOverflow.Ellipsis )