[Android] Audio player global progress: take stop after into account

Nicolas Pomepuy git at videolan.org
Tue Jun 8 12:43:26 UTC 2021


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Fri Jun  4 07:13:09 2021 +0200| [f4f6107f4f778e484feaf51e7f7f70924e661131] | committer: Nicolas Pomepuy

Audio player global progress: take stop after into account

Fixes #2050

> https://code.videolan.org/videolan/vlc-android/commit/f4f6107f4f778e484feaf51e7f7f70924e661131
---

 .../src/org/videolan/vlc/gui/audio/AudioPlayer.kt  | 29 +++++++++++++++-------
 .../src/org/videolan/vlc/media/PlaylistManager.kt  |  1 +
 .../org/videolan/vlc/viewmodels/PlaylistModel.kt   | 26 +++++++------------
 3 files changed, 30 insertions(+), 26 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 c0a4c80e9..dd7eaa1b0 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
@@ -30,8 +30,6 @@ import android.support.v4.media.session.PlaybackStateCompat
 import android.text.Editable
 import android.text.TextWatcher
 import android.text.format.DateFormat
-import android.text.format.DateUtils
-import android.util.Log
 import android.view.LayoutInflater
 import android.view.MotionEvent
 import android.view.View
@@ -364,22 +362,35 @@ class AudioPlayer : Fragment(), PlaylistAdapter.IPlayer, TextWatcher, IAudioPlay
         lifecycleScope.launchWhenStarted {
             val text = withContext(Dispatchers.Default) {
                 val medias = playlistModel.medias ?: return at withContext ""
-                withContext(Dispatchers.Main) { if (!shouldHidePlayProgress()) binding.audioPlayProgress.setVisible() else  binding.audioPlayProgress.setGone() }
+                withContext(Dispatchers.Main) { if (!shouldHidePlayProgress()) binding.audioPlayProgress.setVisible() else binding.audioPlayProgress.setGone() }
                 if (playlistModel.currentMediaPosition == -1) return at withContext ""
                 val elapsedTracksTime = playlistModel.previousTotalTime ?: return at withContext ""
-                val totalTime = elapsedTracksTime + progress.time
-                val totalTimeText = Tools.millisToString(if (showRemainingTime && playlistModel.getTotalTime()>0) playlistModel.getTotalTime() - totalTime else totalTime, false, true, false)
-                val currentProgressText = if (totalTimeText.isNullOrEmpty()) "0:00" else totalTimeText
+                val progressTime = elapsedTracksTime + progress.time
+                val totalTime = playlistModel.getTotalTime()
+                val progressTimeText = Tools.millisToString(
+                    if (showRemainingTime && totalTime > 0) totalTime - progressTime else progressTime,
+                    false,
+                    true,
+                    false
+                )
+                val totalTimeText = Tools.millisToString(totalTime, false, false, false)
+                val currentProgressText = if (progressTimeText.isNullOrEmpty()) "0:00" else progressTimeText
 
                 val textTrack = getString(R.string.track_index, "${playlistModel.currentMediaPosition + 1} / ${medias.size}")
                 val textProgress = if (audioPlayProgressMode)
-                        getString(R.string.audio_queue_progress_finished,DateFormat.format("hh:mm:ss a",  Date(System.currentTimeMillis() + playlistModel.getTotalTime() - totalTime)))
+                    getString(
+                        R.string.audio_queue_progress_finished,
+                        DateFormat.format("hh:mm:ss a", Date(System.currentTimeMillis() + totalTime - progressTime))
+                    )
                 else
-                    if (showRemainingTime && playlistModel.getTotalTime() > 0) getString(R.string.audio_queue_progress_remaining, "$currentProgressText")
+                    if (showRemainingTime && totalTime > 0) getString(
+                        R.string.audio_queue_progress_remaining,
+                        "$currentProgressText"
+                    )
                     else
                         getString(
                             R.string.audio_queue_progress,
-                            if (playlistModel.totalTime.isNullOrEmpty()) "$currentProgressText" else "$currentProgressText / ${playlistModel.totalTime}"
+                            if (totalTimeText.isNullOrEmpty()) "$currentProgressText" else "$currentProgressText / $totalTimeText"
                         )
                 "$textTrack  •  $textProgress"
             }
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 d81e0952d..db791e29f 100644
--- a/application/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
+++ b/application/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
@@ -139,6 +139,7 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
         val instance = Settings.getInstance(AppContextProvider.appContext)
         if (!avoidErasingStop) instance.putSingle(AUDIO_STOP_AFTER, -1)
         stopAfter = instance.getInt(AUDIO_STOP_AFTER, -1)
+        if (stopAfter < position) stopAfter = -1
         clearABRepeat()
         player.setRate(1.0f, false)
         playIndex(currentIndex)
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 35127c6ef..1d5250481 100644
--- a/application/vlc-android/src/org/videolan/vlc/viewmodels/PlaylistModel.kt
+++ b/application/vlc-android/src/org/videolan/vlc/viewmodels/PlaylistModel.kt
@@ -51,7 +51,6 @@ class PlaylistModel : ViewModel(), PlaybackService.Callback by EmptyPBSCallback
     val progress = MediatorLiveData<PlaybackProgress>()
     val speed = MediatorLiveData<Float>()
     val playerState = MutableLiveData<PlayerState>()
-    var totalTime = ""
     val connected : Boolean
         get() = service !== null
 
@@ -75,7 +74,6 @@ class PlaylistModel : ViewModel(), PlaybackService.Callback by EmptyPBSCallback
     }
 
     override fun update() {
-        updateTotalTime()
         service?.run {
             dataset.value = media.toMutableList()
             playerState.value = PlayerState(isPlaying, title, artist)
@@ -238,21 +236,15 @@ class PlaylistModel : ViewModel(), PlaybackService.Callback by EmptyPBSCallback
         }
     }
 
-    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, false, false, false)
-    }
-
-    fun getTotalTime():Long {
-            val mediaList = medias ?: return 0L
-            return mediaList.asSequence()
-                    .map { it.length }
-                    .sum()
+    fun getTotalTime(): Long {
+        val mediaList = medias ?: return 0L
+        return mediaList.asSequence()
+            .map {
+                val stopAfter = service?.playlistManager?.stopAfter
+                if (stopAfter == null || stopAfter == -1 || mediaList.indexOf(it) <= stopAfter)
+                    it.length else 0L
+            }
+            .sum()
     }
 
     companion object {



More information about the Android mailing list