[Android] Improve load last playlist

Geoffrey Métais git at videolan.org
Tue Mar 31 13:50:35 CEST 2020


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Tue Mar 31 11:54:55 2020 +0200| [0c79b8cb0d090dc0b1918d52af388108e15fff2c] | committer: Nicolas Pomepuy

Improve load last playlist

Speed playback start by defering medialibrary load
Update notification once medialibrary meta are loaded

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

 .../src/org/videolan/vlc/PlaybackService.kt        |  7 +--
 .../src/org/videolan/vlc/media/PlaylistManager.kt  | 58 +++++++++++-----------
 2 files changed, 30 insertions(+), 35 deletions(-)

diff --git a/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt b/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
index 15d2fe781..6120892d8 100644
--- a/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
+++ b/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
@@ -996,10 +996,7 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner {
     }
 
     fun loadLastPlaylist(type: Int) {
-        lifecycleScope.launch {
-            awaitMedialibraryStarted()
-            if (!playlistManager.loadLastPlaylist(type)) stopService(Intent(applicationContext, PlaybackService::class.java))
-        }
+        if (!playlistManager.loadLastPlaylist(type)) stopService(Intent(applicationContext, PlaybackService::class.java))
     }
 
     fun showToast(text: String, duration: Int, isError: Boolean = false) {
@@ -1058,7 +1055,7 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner {
     }
 
     @MainThread
-    fun load(mediaList: List<MediaWrapper>, position: Int) = playlistManager.load(mediaList, position)
+    fun load(mediaList: List<MediaWrapper>, position: Int) = lifecycleScope.launch { playlistManager.load(mediaList, position) }
 
     private fun updateMediaQueue() = lifecycleScope.launch(start = CoroutineStart.UNDISPATCHED) {
         if (!this at PlaybackService::mediaSession.isInitialized) initMediaSession()
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 7a8071346..098769a65 100644
--- a/application/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
+++ b/application/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
@@ -27,10 +27,8 @@ import org.videolan.vlc.BuildConfig
 import org.videolan.vlc.PlaybackService
 import org.videolan.vlc.R
 import org.videolan.vlc.gui.video.VideoPlayerActivity
+import org.videolan.vlc.util.*
 import org.videolan.vlc.util.FileUtils
-import org.videolan.vlc.util.setResumeProgram
-import org.videolan.vlc.util.updateWithMLMeta
-import org.videolan.vlc.util.validateLocation
 import java.util.*
 import kotlin.math.max
 
@@ -122,31 +120,31 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
     }
 
     @MainThread
-    fun load(list: List<MediaWrapper>, position: Int, mlUpdate: Boolean = false) {
-        launch {
-            saveMediaList()
-            savePosition()
-            mediaList.removeEventListener(this at PlaylistManager)
-            previous.clear()
-            videoBackground = false
-            mediaList.replaceWith(list)
-            if (!hasMedia()) {
-                Log.w(TAG, "Warning: empty media list, nothing to play !")
-                return at launch
-            }
-            currentIndex = if (isValidPosition(position)) position else 0
-
-            // Add handler after loading the list
-            mediaList.addEventListener(this at PlaylistManager)
-            stopAfter = -1
-            clearABRepeat()
-            player.setRate(1.0f, false)
-            playIndex(currentIndex)
-            service.onPlaylistLoaded()
-            if (mlUpdate) {
-                mediaList.replaceWith(withContext(Dispatchers.IO) { mediaList.copy.updateWithMLMeta() })
-                executeUpdate()
-            }
+    suspend fun load(list: List<MediaWrapper>, position: Int, mlUpdate: Boolean = false) {
+        saveMediaList()
+        savePosition()
+        mediaList.removeEventListener(this at PlaylistManager)
+        previous.clear()
+        videoBackground = false
+        mediaList.replaceWith(list)
+        if (!hasMedia()) {
+            Log.w(TAG, "Warning: empty media list, nothing to play !")
+            return
+        }
+        currentIndex = if (isValidPosition(position)) position else 0
+
+        // Add handler after loading the list
+        mediaList.addEventListener(this at PlaylistManager)
+        stopAfter = -1
+        clearABRepeat()
+        player.setRate(1.0f, false)
+        playIndex(currentIndex)
+        service.onPlaylistLoaded()
+        if (mlUpdate) {
+            service.awaitMedialibraryStarted()
+            mediaList.replaceWith(withContext(Dispatchers.IO) { mediaList.copy.updateWithMLMeta() })
+            executeUpdate()
+            service.showNotification()
         }
     }
 
@@ -664,7 +662,7 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
     @MainThread
     suspend fun append(list: List<MediaWrapper>) {
         if (!hasCurrentMedia()) {
-            load(list, 0)
+            launch { load(list, 0, mlUpdate = true) }
             return
         }
         val list = withContext(Dispatchers.IO) { list.updateWithMLMeta() }
@@ -681,7 +679,7 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
     @MainThread
     fun insertNext(list: List<MediaWrapper>) {
         if (!hasCurrentMedia()) {
-            load(list, 0)
+            launch { load(list, 0) }
             return
         }
         val startIndex = currentIndex + 1



More information about the Android mailing list