make seeker show playback position

This commit is contained in:
Christian Schabesberger 2024-07-26 14:41:34 +02:00
parent e44d76f1d8
commit 7acfd77df2
2 changed files with 30 additions and 6 deletions

View File

@ -110,6 +110,7 @@ class VideoPlayerViewModelImpl @Inject constructor(
private val mutableUiState = MutableStateFlow(VideoPlayerUIState.DEFAULT) private val mutableUiState = MutableStateFlow(VideoPlayerUIState.DEFAULT)
private var currentContentRatio = 1F private var currentContentRatio = 1F
private var uiVisibilityJob: Job? = null private var uiVisibilityJob: Job? = null
private var progressUpdaterJob: Job? = null
//interface //interface
override var callbackListener: VideoPlayerViewModel.Listener? = null override var callbackListener: VideoPlayerViewModel.Listener? = null
@ -227,12 +228,12 @@ class VideoPlayerViewModelImpl @Inject constructor(
} }
override fun prevStream() { override fun prevStream() {
resetHideUiDelayed() resetHideUiDelayedJob()
Log.e(TAG, "imeplement prev stream") Log.e(TAG, "imeplement prev stream")
} }
override fun nextStream() { override fun nextStream() {
resetHideUiDelayed() resetHideUiDelayedJob()
Log.e(TAG, "implement next stream") Log.e(TAG, "implement next stream")
} }
@ -243,10 +244,11 @@ class VideoPlayerViewModelImpl @Inject constructor(
mutableUiState.update { mutableUiState.update {
it.copy(uiVissible = true) it.copy(uiVissible = true)
} }
resetHideUiDelayed() resetHideUiDelayedJob()
resetProgressUpdatePeriodicallyJob()
} }
private fun resetHideUiDelayed() { private fun resetHideUiDelayedJob() {
uiVisibilityJob?.cancel() uiVisibilityJob?.cancel()
uiVisibilityJob = viewModelScope.launch { uiVisibilityJob = viewModelScope.launch {
delay(4000) delay(4000)
@ -254,10 +256,32 @@ class VideoPlayerViewModelImpl @Inject constructor(
} }
} }
private fun resetProgressUpdatePeriodicallyJob() {
progressUpdaterJob?.cancel()
progressUpdaterJob = viewModelScope.launch {
while(true) {
updateProgressOnce()
delay(1000)
}
}
}
private fun updateProgressOnce() {
val progress = player?.currentPosition ?: 0
val duration = player?.duration ?: 1
val progressPercentage = progress.toFloat() / duration.toFloat()
mutableUiState.update {
it.copy(seekerPosition = progressPercentage)
}
Log.i(TAG, "Progress: $progress, Duration: $duration")
}
override fun hideUi() { override fun hideUi() {
if (mutableUiState.value.fullscreen) if (mutableUiState.value.fullscreen)
callbackListener?.onUiVissibleToggle(false) callbackListener?.onUiVissibleToggle(false)
progressUpdaterJob?.cancel()
uiVisibilityJob?.cancel() uiVisibilityJob?.cancel()
mutableUiState.update { mutableUiState.update {
it.copy(uiVissible = false) it.copy(uiVissible = false)
@ -270,7 +294,7 @@ class VideoPlayerViewModelImpl @Inject constructor(
} }
override fun seekingFinished() { override fun seekingFinished() {
resetHideUiDelayed() resetHideUiDelayedJob()
Log.d(TAG, "TODO: Implement seeking Finished") Log.d(TAG, "TODO: Implement seeking Finished")
} }

View File

@ -59,7 +59,7 @@ class MainActivity : AppCompatActivity() {
startStreamButton.setOnClickListener { startStreamButton.setOnClickListener {
newPlayer.playWhenReady = true newPlayer.playWhenReady = true
newPlayer.setStream(getString(R.string.ccc_6502_video)) newPlayer.setStream(getString(R.string.portrait_video_example))
} }
videoPlayerViewModel.newPlayer = newPlayer videoPlayerViewModel.newPlayer = newPlayer