[Android] Compute progress in background
Geoffrey Métais
git at videolan.org
Mon Feb 24 10:10:36 CET 2020
vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Mon Feb 24 10:09:02 2020 +0100| [953bfaea4e5bed3ab7a3c16ce5076dbbfe221146] | committer: Geoffrey Métais
Compute progress in background
> https://code.videolan.org/videolan/vlc-android/commit/953bfaea4e5bed3ab7a3c16ce5076dbbfe221146
---
.../src/org/videolan/vlc/gui/audio/AudioPlayer.kt | 29 +++++++++++++---------
.../org/videolan/vlc/viewmodels/PlaylistModel.kt | 11 +++++---
2 files changed, 24 insertions(+), 16 deletions(-)
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.kt b/application/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.kt
index c46d7d4d7..b6609feba 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.kt
@@ -302,19 +302,24 @@ class AudioPlayer : Fragment(), PlaylistAdapter.IPlayer, TextWatcher, IAudioPlay
binding.timeline.progress = progress.time.toInt()
binding.progressBar.progress = progress.time.toInt()
}
-
- val elapsedTracksTime = playlistModel.medias?.asSequence()
- ?.take(playlistModel.currentMediaPosition)
- ?.map { if (it.length != 0L) it.length else it.time }
- ?.sum() ?: 0L
- val totalTime = elapsedTracksTime + progress.time
- val currentProgressText = if (totalTime == 0L) "0s" else Tools.millisToString(totalTime, true, false, false)
-
- val textTrack = getString(R.string.track_index, "${playlistModel.currentMediaPosition + 1} / ${playlistModel.medias?.size}")
- val textProgress = getString(R.string.audio_queue_progress, "$currentProgressText / ${playlistModel.totalTime}")
-
- binding.audioPlayProgress.text = "$textTrack • $textProgress"
binding.songsList.setPadding(binding.songsList.paddingLeft, binding.songsList.paddingTop, binding.songsList.paddingRight, binding.audioPlayProgress.height + 8.dp)
+
+ lifecycleScope.launchWhenStarted {
+ val text = withContext(Dispatchers.Default) {
+ val medias = playlistModel.medias ?: return at withContext ""
+ val elapsedTracksTime = medias.asSequence()
+ .take(playlistModel.currentMediaPosition)
+ .map { it.length }
+ .sum()
+ val totalTime = elapsedTracksTime + progress.time
+ val currentProgressText = if (totalTime == 0L) "0s" else Tools.millisToString(totalTime, true, false, false)
+
+ val textTrack = getString(R.string.track_index, "${playlistModel.currentMediaPosition + 1} / ${medias.size}")
+ val textProgress = getString(R.string.audio_queue_progress, "$currentProgressText / ${playlistModel.totalTime}")
+ "$textTrack • $textProgress"
+ }
+ binding.audioPlayProgress.text = text
+ }
}
override fun onSelectionSet(position: Int) {
diff --git a/application/vlc-android/src/org/videolan/vlc/viewmodels/PlaylistModel.kt b/application/vlc-android/src/org/videolan/vlc/viewmodels/PlaylistModel.kt
index 96f15b68b..993146d6d 100644
--- a/application/vlc-android/src/org/videolan/vlc/viewmodels/PlaylistModel.kt
+++ b/application/vlc-android/src/org/videolan/vlc/viewmodels/PlaylistModel.kt
@@ -225,10 +225,13 @@ class PlaylistModel : ViewModel(), PlaybackService.Callback by EmptyPBSCallback
}
}
- private fun updateTotalTime() = viewModelScope.launch{
- val totalLength = medias?.asSequence()?.map {
- if (it.length != 0L) it.length else it.time
- }?.sum() ?: 0L
+ private fun updateTotalTime() = viewModelScope.launch {
+ val totalLength = withContext(Dispatchers.Default) {
+ val mediaList = medias ?: return at withContext 0L
+ mediaList.asSequence()
+ .map { it.length }
+ .sum()
+ }
totalTime = Tools.millisToString(totalLength, true, false, false)
}
More information about the Android
mailing list