[Android] Prevent media change on resume

Nicolas Pomepuy git at videolan.org
Tue Apr 13 06:33:46 UTC 2021


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Thu Apr  8 10:57:37 2021 +0200| [35f1dedfb01f06b958ca7e23d5ea5bfb2a11d9e2] | committer: Nicolas Pomepuy

Prevent media change on resume

Fixes #1964
Fixes #1653

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

 .../main/java/org/videolan/resources/Constants.kt  |  3 +++
 .../videolan/vlc/gui/video/VideoPlayerActivity.kt  |  2 +-
 .../src/org/videolan/vlc/media/PlaylistManager.kt  | 30 +++++++++++++++++-----
 3 files changed, 28 insertions(+), 7 deletions(-)

diff --git a/application/resources/src/main/java/org/videolan/resources/Constants.kt b/application/resources/src/main/java/org/videolan/resources/Constants.kt
index 6dd122b12..542838cfa 100644
--- a/application/resources/src/main/java/org/videolan/resources/Constants.kt
+++ b/application/resources/src/main/java/org/videolan/resources/Constants.kt
@@ -59,6 +59,7 @@ val ACTION_REMOTE_GENERIC = "remote.".buildPkgString()
 const val ACTION_CAR_MODE_EXIT = "android.app.action.EXIT_CAR_MODE"
 const val PLAYLIST_TYPE_AUDIO = 0
 const val PLAYLIST_TYPE_VIDEO = 1
+const val PLAYLIST_TYPE_VIDEO_RESUME = 2
 const val MEDIALIBRARY_PAGE_SIZE = 500
 const val PLAYBACK_SLOT_RESERVATION_SKIP_TO_NEXT = "android.media.playback.ALWAYS_RESERVE_SPACE_FOR.ACTION_SKIP_TO_NEXT"
 const val PLAYBACK_SLOT_RESERVATION_SKIP_TO_PREV = "android.media.playback.ALWAYS_RESERVE_SPACE_FOR.ACTION_SKIP_TO_PREVIOUS"
@@ -104,8 +105,10 @@ const val KEY_AUDIO_CURRENT_TAB = "key_audio_current_tab"
 // Playback settings category
 const val KEY_AUDIO_LAST_PLAYLIST = "audio_list"
 const val KEY_MEDIA_LAST_PLAYLIST = "media_list"
+const val KEY_MEDIA_LAST_PLAYLIST_RESUME = "media_list_resume"
 const val KEY_CURRENT_AUDIO = "current_song"
 const val KEY_CURRENT_MEDIA = "current_media"
+const val KEY_CURRENT_MEDIA_RESUME = "current_media_resume"
 
 // Info Activity
 const val TAG_ITEM = "ML_ITEM"
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 b58c5bcac..16bb5cec8 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
@@ -1819,7 +1819,7 @@ open class VideoPlayerActivity : AppCompatActivity(), PlaybackService.Callback,
             } else if (service.hasMedia() && !displayManager.isPrimary) {
                 onPlaying()
             } else {
-                service.loadLastPlaylist(PLAYLIST_TYPE_VIDEO)
+                service.loadLastPlaylist(PLAYLIST_TYPE_VIDEO_RESUME)
             }
             if (itemTitle != null) title = itemTitle
             if (overlayDelegate.isHudRightBindingInitialized()) {
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 355601cd4..7ee31b945 100644
--- a/application/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
+++ b/application/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
@@ -156,13 +156,23 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
     fun loadLastPlaylist(type: Int = PLAYLIST_TYPE_AUDIO) : Boolean {
         if (loadingLastPlaylist) return true
         loadingLastPlaylist = true
+        val currentMediaKey = when (type) {
+            PLAYLIST_TYPE_AUDIO -> KEY_CURRENT_AUDIO
+            PLAYLIST_TYPE_VIDEO -> KEY_CURRENT_MEDIA
+            else -> KEY_CURRENT_MEDIA_RESUME
+        }
+        val locationsKey = when (type) {
+            PLAYLIST_TYPE_AUDIO -> KEY_AUDIO_LAST_PLAYLIST
+            PLAYLIST_TYPE_VIDEO -> KEY_MEDIA_LAST_PLAYLIST
+            else -> KEY_MEDIA_LAST_PLAYLIST_RESUME
+        }
         val audio = type == PLAYLIST_TYPE_AUDIO
-        val currentMedia = settings.getString(if (audio) KEY_CURRENT_AUDIO else KEY_CURRENT_MEDIA, "")
+        val currentMedia = settings.getString(currentMediaKey, "")
         if (currentMedia.isNullOrEmpty()) {
             loadingLastPlaylist = false
             return false
         }
-        val locations = settings.getString(if (audio) KEY_AUDIO_LAST_PLAYLIST else KEY_MEDIA_LAST_PLAYLIST, null)
+        val locations = settings.getString(locationsKey, null)
                 ?.split(" ".toRegex())?.dropLastWhile { it.isEmpty() }?.toTypedArray()
         if (locations.isNullOrEmpty()) {
             loadingLastPlaylist = false
@@ -199,6 +209,11 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
     fun pause() {
         if (player.pause()) {
             savePosition()
+            launch {
+                //needed to save the current media in audio mode when it's a video played as audio
+                saveCurrentMedia()
+                saveMediaList()
+            }
             if (getCurrentMedia()?.isPodcast == true) saveMediaMeta()
         }
     }
@@ -510,20 +525,23 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
     }
 
     @Synchronized
-    private fun saveCurrentMedia() {
+    fun saveCurrentMedia(forceVideo:Boolean = false) {
         val media = getCurrentMedia() ?: return
-        val isAudio = isAudioList()
+        val isAudio = isAudioList() || forceVideo
         settings.putSingle(if (isAudio) KEY_CURRENT_AUDIO else KEY_CURRENT_MEDIA, media.location)
+        settings.putSingle(KEY_CURRENT_MEDIA_RESUME, media.location)
     }
 
-    private suspend fun saveMediaList() {
+    suspend fun saveMediaList(forceVideo:Boolean = false) {
         if (getCurrentMedia() === null) return
         val locations = StringBuilder()
+        val isAudio = isAudioList() || forceVideo
         withContext(Dispatchers.Default) {
             val list = mediaList.copy.takeIf { it.isNotEmpty() } ?: return at withContext
             for (mw in list) locations.append(" ").append(mw.uri.toString())
             //We save a concatenated String because putStringSet is APIv11.
-            settings.putSingle(if (isAudioList()) KEY_AUDIO_LAST_PLAYLIST else KEY_MEDIA_LAST_PLAYLIST, locations.toString().trim())
+            settings.putSingle(if (isAudio) KEY_AUDIO_LAST_PLAYLIST else KEY_MEDIA_LAST_PLAYLIST, locations.toString().trim())
+            settings.putSingle(KEY_MEDIA_LAST_PLAYLIST_RESUME, locations.toString().trim())
         }
     }
 



More information about the Android mailing list