[Android] Persist track to stop after to avoid loss on service kill

Nicolas Pomepuy git at videolan.org
Thu Apr 1 13:07:37 UTC 2021


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Tue Mar 30 15:13:51 2021 +0200| [70ed134083dd65c4cb63b1e9de25cdaf2b0a3b84] | committer: Nicolas Pomepuy

Persist track to stop after to avoid loss on service kill

Fixes #1668

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

 application/tools/src/main/java/org/videolan/tools/Settings.kt |  1 +
 .../vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt  | 10 +++++++---
 2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/application/tools/src/main/java/org/videolan/tools/Settings.kt b/application/tools/src/main/java/org/videolan/tools/Settings.kt
index 64c4ab128..3a31744dd 100644
--- a/application/tools/src/main/java/org/videolan/tools/Settings.kt
+++ b/application/tools/src/main/java/org/videolan/tools/Settings.kt
@@ -103,6 +103,7 @@ const val AUDIO_DUCKING = "audio_ducking"
 
 const val AUDIO_DELAY_GLOBAL = "audio_delay_global"
 const val AUDIO_PLAY_PROGRESS_MODE = "audio_play_progress_mode"
+const val AUDIO_STOP_AFTER = "audio_stop_after"
 
 class DeviceInfo(context: Context) {
     val pm = context.packageManager
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 65419f99d..5d4da0a7d 100644
--- a/application/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
+++ b/application/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
@@ -120,7 +120,7 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
     }
 
     @MainThread
-    suspend fun load(list: List<MediaWrapper>, position: Int, mlUpdate: Boolean = false) {
+    suspend fun load(list: List<MediaWrapper>, position: Int, mlUpdate: Boolean = false, avoidErasingStop:Boolean = false) {
         saveMediaList()
         savePosition()
         mediaList.removeEventListener(this at PlaylistManager)
@@ -135,7 +135,9 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
 
         // Add handler after loading the list
         mediaList.addEventListener(this at PlaylistManager)
-        stopAfter = -1
+        val instance = Settings.getInstance(AppContextProvider.appContext)
+        if (!avoidErasingStop) instance.putSingle(AUDIO_STOP_AFTER, -1)
+        stopAfter = instance.getInt(AUDIO_STOP_AFTER, -1)
         clearABRepeat()
         player.setRate(1.0f, false)
         playIndex(currentIndex)
@@ -179,7 +181,7 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
                 playList[position].addFlags(MediaWrapper.MEDIA_PAUSED)
             }
             if (audio && position < playList.size) playList[position].addFlags(MediaWrapper.MEDIA_FORCE_AUDIO)
-            load(playList, position, true)
+            load(playList, position, true, true)
             loadingLastPlaylist = false
             if (!audio) {
                 val rate = settings.getFloat(VIDEO_SPEED, player.getRate())
@@ -227,6 +229,7 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
 
     fun stop(systemExit: Boolean = false, video: Boolean = false) {
         clearABRepeat()
+        if (stopAfter != -1) Settings.getInstance(AppContextProvider.appContext).putSingle(AUDIO_STOP_AFTER, stopAfter)
         stopAfter = -1
         videoBackground = false
         val job = getCurrentMedia()?.let {
@@ -836,6 +839,7 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
                         if (nextIndex == -1) savePosition(reset = true)
                     }
                     if (stopAfter == currentIndex) {
+                        if (BuildConfig.DEBUG) Log.d("AUDIO_STOP_AFTER", "reset")
                         stop()
                     } else {
                         if (isBenchmark) player.setCurrentStats()



More information about the Android mailing list