[Android] Fix audio player wrong elapsed time when shuffling
Nicolas Pomepuy
git at videolan.org
Thu Jul 23 13:15:37 CEST 2020
vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Thu Jul 23 08:01:19 2020 +0200| [76855c9cd701410120de0015e0f1f72e7f14e8d1] | committer: Nicolas Pomepuy
Fix audio player wrong elapsed time when shuffling
Fixes #1423
> https://code.videolan.org/videolan/vlc-android/commit/76855c9cd701410120de0015e0f1f72e7f14e8d1
---
.../vlc-android/src/org/videolan/vlc/PlaybackService.kt | 4 ++++
.../src/org/videolan/vlc/gui/audio/AudioPlayer.kt | 5 +----
.../src/org/videolan/vlc/media/PlaylistManager.kt | 12 ++++++++++++
.../src/org/videolan/vlc/viewmodels/PlaylistModel.kt | 3 +++
4 files changed, 20 insertions(+), 4 deletions(-)
diff --git a/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt b/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
index 8ae326c9d..be3127ede 100644
--- a/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
+++ b/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
@@ -346,6 +346,10 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner {
@MainThread
get() = playlistManager.getMediaList()
+ val previousTotalTime
+ @MainThread
+ get() = playlistManager.previousTotalTime()
+
val mediaLocations: List<String>
@MainThread
get() {
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 70d21eaee..53d689341 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
@@ -310,10 +310,7 @@ class AudioPlayer : Fragment(), PlaylistAdapter.IPlayer, TextWatcher, IAudioPlay
val text = withContext(Dispatchers.Default) {
val medias = playlistModel.medias ?: return at withContext ""
if (playlistModel.currentMediaPosition == -1) return at withContext ""
- val elapsedTracksTime = medias.asSequence()
- .take(playlistModel.currentMediaPosition)
- .map { it.length }
- .sum()
+ val elapsedTracksTime = playlistModel.previousTotalTime ?: return at withContext ""
val totalTime = elapsedTracksTime + progress.time
val currentProgressText = if (totalTime == 0L) "0s" else Tools.millisToString(totalTime, true, false, false)
diff --git a/application/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt b/application/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
index d4b13f12a..190d8e055 100644
--- a/application/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
+++ b/application/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
@@ -578,6 +578,18 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
}
}
+ fun previousTotalTime() = if (shuffling) {
+ mediaList.copy.asSequence()
+ .filterIndexed { index, _ -> previous.contains(index) }
+ .map { it.length }
+ .sum()
+ } else {
+ mediaList.copy.asSequence()
+ .take(currentIndex)
+ .map { it.length }
+ .sum()
+ }
+
/**
* Expand the current media.
* @return the index of the media was expanded, and -1 if no media was expanded
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 b496fdfb3..14523f0b6 100644
--- a/application/vlc-android/src/org/videolan/vlc/viewmodels/PlaylistModel.kt
+++ b/application/vlc-android/src/org/videolan/vlc/viewmodels/PlaylistModel.kt
@@ -183,6 +183,9 @@ class PlaylistModel : ViewModel(), PlaybackService.Callback by EmptyPBSCallback
val medias
get() = service?.media
+ val previousTotalTime
+ get() = service?.previousTotalTime
+
fun shuffle() = service?.shuffle()
fun load(medialist: List<MediaWrapper>, position: Int) = service?.load(medialist, position)
More information about the Android
mailing list