[Android] VideoPlayer: save media index when leaving app.

Duncan McNamara git at videolan.org
Tue Oct 18 12:12:00 UTC 2022


vlc-android | branch: 3.5.x | Duncan McNamara <dcn.mcnamara at gmail.com> | Mon Oct 17 15:19:33 2022 +0200| [99adb7bcb7df2eb7499a2a3d946e564314d30523] | committer: Nicolas Pomepuy

VideoPlayer: save media index when leaving app.

On some low performance devices, when leaving the app, the paused
service will be killed, and respawned when the app is restarted.
As the medialist was saved but not the index, resuming would go to
the 1st media in the saved medialist. This saves the media index
also.

Fixes #2705


(cherry picked from commit 72ed253419de5be64ca244e5d932920fa04567a2)

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

 application/vlc-android/src/org/videolan/vlc/PlaybackService.kt    | 6 +++---
 .../src/org/videolan/vlc/gui/video/VideoPlayerActivity.kt          | 7 ++++++-
 .../vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt      | 4 ++--
 3 files changed, 11 insertions(+), 6 deletions(-)

diff --git a/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt b/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
index 9a2ef5b34..8a94fb53f 100644
--- a/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
+++ b/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
@@ -1513,11 +1513,11 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner, CoroutineSc
      */
 
     @MainThread
-    fun append(mediaList: Array<MediaWrapper>) = append(mediaList.toList())
+    fun append(mediaList: Array<MediaWrapper>, index: Int = 0) = append(mediaList.toList(), index)
 
     @MainThread
-    fun append(mediaList: List<MediaWrapper>) = lifecycleScope.launch(start = CoroutineStart.UNDISPATCHED) {
-        playlistManager.append(mediaList)
+    fun append(mediaList: List<MediaWrapper>, index: Int = 0) = lifecycleScope.launch(start = CoroutineStart.UNDISPATCHED) {
+        playlistManager.append(mediaList, index)
         onMediaListChanged()
     }
 
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.kt b/application/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.kt
index b59dbae02..367e6fabf 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.kt
@@ -130,6 +130,7 @@ open class VideoPlayerActivity : AppCompatActivity(), PlaybackService.Callback,
     var rootView: View? = null
     var videoUri: Uri? = null
     var savedMediaList: ArrayList<MediaWrapper>? = null
+    var savedMediaIndex: Int = 0
     private var askResume = true
 
     var playlistModel: PlaylistModel? = null
@@ -487,6 +488,7 @@ open class VideoPlayerActivity : AppCompatActivity(), PlaybackService.Callback,
         if (savedInstanceState != null) {
             savedTime = savedInstanceState.getLong(KEY_TIME)
             savedMediaList = savedInstanceState.getParcelableArrayList(KEY_MEDIA_LIST)
+            savedMediaIndex = savedInstanceState.getInt(KEY_MEDIA_INDEX)
             val list = savedInstanceState.getBoolean(KEY_LIST, false)
             if (list) {
                 intent.removeExtra(PLAY_EXTRA_ITEM_LOCATION)
@@ -660,8 +662,10 @@ open class VideoPlayerActivity : AppCompatActivity(), PlaybackService.Callback,
             if (playlistModel == null) outState.putParcelable(KEY_URI, videoUri)
         }
         val mediaList = service?.playlistManager?.getMediaList() ?: savedMediaList
+        val mediaIndex = service?.playlistManager?.currentIndex ?: 0
         if (mediaList != null) {
             outState.putParcelableArrayList(KEY_MEDIA_LIST, ArrayList(mediaList))
+            outState.putInt(KEY_MEDIA_INDEX, mediaIndex)
             savedMediaList = null
         }
         videoUri = null
@@ -2238,7 +2242,7 @@ open class VideoPlayerActivity : AppCompatActivity(), PlaybackService.Callback,
         if (service != null) {
             this.service = service
             if (savedMediaList != null && service.currentMediaWrapper == null) {
-                service.append(savedMediaList!!)
+                service.append(savedMediaList!!, savedMediaIndex)
                 savedMediaList = null
             }
             //We may not have the permission to access files
@@ -2294,6 +2298,7 @@ open class VideoPlayerActivity : AppCompatActivity(), PlaybackService.Callback,
         private const val KEY_TIME = "saved_time"
         private const val KEY_LIST = "saved_list"
         private const val KEY_MEDIA_LIST = "media_list"
+        private const val KEY_MEDIA_INDEX = "media_index"
         private const val KEY_URI = "saved_uri"
         const val OVERLAY_INFINITE = -1
         const val FADE_OUT = 1
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 8db40502f..9a59d2f88 100644
--- a/application/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
+++ b/application/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
@@ -846,10 +846,10 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
      * Append to the current existing playlist
      */
     @MainThread
-    suspend fun append(list: List<MediaWrapper>) {
+    suspend fun append(list: List<MediaWrapper>, index: Int = 0) {
         if (BuildConfig.BETA) Log.d(TAG, "append with values: ", Exception("Call stack"))
         if (!hasCurrentMedia()) {
-            launch { load(list, 0, mlUpdate = true) }
+            launch { load(list, index, mlUpdate = true) }
             return
         }
         val list = withContext(Dispatchers.IO) { list.updateWithMLMeta() }



More information about the Android mailing list