update playlist playtime

This commit is contained in:
Christian Schabesberger 2024-09-04 15:13:21 +02:00
parent a2e9675906
commit 3eabb537f7
3 changed files with 37 additions and 6 deletions

View File

@ -36,6 +36,7 @@ data class VideoPlayerUIState(
val isLoading: Boolean, val isLoading: Boolean,
val durationInMs: Long, val durationInMs: Long,
val playbackPositionInMs: Long, val playbackPositionInMs: Long,
val playbackPositionInPlaylistS: Int,
val fastSeekSeconds: Int, val fastSeekSeconds: Int,
val soundVolume: Float, val soundVolume: Float,
val brightness: Float?, // when null use system value val brightness: Float?, // when null use system value
@ -58,6 +59,7 @@ data class VideoPlayerUIState(
bufferedPercentage = 0f, bufferedPercentage = 0f,
isLoading = true, isLoading = true,
durationInMs = 0, durationInMs = 0,
playbackPositionInPlaylistS = 0,
playbackPositionInMs = 0, playbackPositionInMs = 0,
fastSeekSeconds = 0, fastSeekSeconds = 0,
soundVolume = 0f, soundVolume = 0f,
@ -78,6 +80,7 @@ data class VideoPlayerUIState(
bufferedPercentage = 0.5f, bufferedPercentage = 0.5f,
isLoading = false, isLoading = false,
durationInMs = 420, durationInMs = 420,
playbackPositionInPlaylistS = 5039,
playbackPositionInMs = 69, playbackPositionInMs = 69,
fastSeekSeconds = 10, fastSeekSeconds = 10,
soundVolume = 0.5f, soundVolume = 0.5f,

View File

@ -30,8 +30,6 @@ import androidx.core.content.ContextCompat.getSystemService
import androidx.lifecycle.AndroidViewModel import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.SavedStateHandle import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import androidx.media3.common.MediaItem
import androidx.media3.common.MediaMetadata
import androidx.media3.common.Player import androidx.media3.common.Player
import dagger.hilt.android.lifecycle.HiltViewModel import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
@ -42,7 +40,6 @@ import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.asSharedFlow import kotlinx.coroutines.flow.asSharedFlow
import javax.inject.Inject import javax.inject.Inject
import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.update import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import net.newpipe.newplayer.Chapter import net.newpipe.newplayer.Chapter
@ -73,6 +70,7 @@ class VideoPlayerViewModelImpl @Inject constructor(
private var uiVisibilityJob: Job? = null private var uiVisibilityJob: Job? = null
private var progressUpdaterJob: 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 // this is necesary to restore the embedded view UI configuration when returning from fullscreen
private var embeddedUiConfig: EmbeddedUiConfig? = null private var embeddedUiConfig: EmbeddedUiConfig? = null
@ -295,7 +293,6 @@ class VideoPlayerViewModelImpl @Inject constructor(
delay(1000) delay(1000)
} }
} }
} }
private fun updateProgressOnce() { private fun updateProgressOnce() {
@ -310,7 +307,33 @@ class VideoPlayerViewModelImpl @Inject constructor(
seekerPosition = progressPercentage, seekerPosition = progressPercentage,
durationInMs = duration, durationInMs = duration,
playbackPositionInMs = progress, 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) { override fun openStreamSelection(selectChapter: Boolean, embeddedUiConfig: EmbeddedUiConfig) {
resetPlaylistProgressUpdaterJob()
uiVisibilityJob?.cancel() uiVisibilityJob?.cancel()
if (!uiState.value.uiMode.fullscreen) { if (!uiState.value.uiMode.fullscreen) {
this.embeddedUiConfig = embeddedUiConfig this.embeddedUiConfig = embeddedUiConfig
@ -407,6 +431,7 @@ class VideoPlayerViewModelImpl @Inject constructor(
} }
override fun closeStreamSelection() { override fun closeStreamSelection() {
playlistProgressUpdatrJob?.cancel()
updateUiMode(uiState.value.uiMode.getUiHiddenState()) updateUiMode(uiState.value.uiMode.getUiHiddenState())
} }

View File

@ -69,8 +69,11 @@ fun StreamSelectTopBar(
val locale = getLocale()!! val locale = getLocale()!!
val duration = getPlaylistDurationInS(uiState.playList).toLong() * 1000 val duration = getPlaylistDurationInS(uiState.playList).toLong() * 1000
val durationString = getTimeStringFromMs(timeSpanInMs = duration, locale) val durationString = getTimeStringFromMs(timeSpanInMs = duration, locale)
val playbackPositionString = getTimeStringFromMs(
timeSpanInMs = uiState.playbackPositionInPlaylistS.toLong() * 1000, locale = locale
)
Text( Text(
text = "00:00/$durationString", text = "$playbackPositionString/$durationString",
maxLines = 1, maxLines = 1,
overflow = TextOverflow.Ellipsis overflow = TextOverflow.Ellipsis
) )