[Android] PlaylistManager: do not block main thread

Geoffrey Métais git at videolan.org
Thu Jan 10 15:06:00 CET 2019


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Jan 10 13:58:58 2019 +0100| [febf38b3e9eacd99bd787124c67323fc895482be] | committer: Geoffrey Métais

PlaylistManager: do not block main thread

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

 vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt b/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
index 821510491..4e387b49d 100644
--- a/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
+++ b/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
@@ -300,7 +300,7 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
                 }
             }
             media.setEventListener(this at PlaylistManager)
-            player.startPlayback(media, mediaplayerEventListener)
+            withContext(Dispatchers.Main) { player.startPlayback(media, mediaplayerEventListener) }
             player.setSlaves(media, mw)
             newMedia = true
             determinePrevAndNextIndices()
@@ -466,11 +466,14 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
     private fun saveMediaList() {
         if (getCurrentMedia() === null) return
         val locations = StringBuilder()
-        for (mw in mediaList.all) locations.append(" ").append(Uri.encode(Uri.decode(mw.uri.toString())))
-        //We save a concatenated String because putStringSet is APIv11.
-        settings.edit()
-                .putString(if (isAudioList()) "audio_list" else "media_list", locations.toString().trim())
-                .apply()
+        launch(Dispatchers.Main) {
+            val list = mediaList.all.takeIf { it.isNotEmpty() } ?: return at launch
+            for (mw in list) locations.append(" ").append(Uri.encode(Uri.decode(mw.uri.toString())))
+            //We save a concatenated String because putStringSet is APIv11.
+            settings.edit()
+                    .putString(if (isAudioList()) "audio_list" else "media_list", locations.toString().trim())
+                    .apply()
+        }
     }
 
     override fun onItemMoved(indexBefore: Int, indexAfter: Int, mrl: String?) {



More information about the Android mailing list