[Android] Avoid IO operations in main thread

Geoffrey Métais git at videolan.org
Thu Jan 24 15:31:27 CET 2019


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Jan 24 15:20:17 2019 +0100| [4ad685337ee2209dbce7fc8158d2d4df844bdee3] | committer: Geoffrey Métais

Avoid IO operations in main thread

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

 .../src/org/videolan/vlc/media/PlaylistManager.kt  | 36 ++++++++++++----------
 1 file changed, 19 insertions(+), 17 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt b/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
index 37f14456c..4979961b5 100644
--- a/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
+++ b/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
@@ -86,22 +86,25 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
      */
     @MainThread
     fun loadLocations(mediaPathList: List<String>, position: Int) {
-        val mediaList = ArrayList<MediaWrapper>()
-
-        for (location in mediaPathList) {
-            var mediaWrapper = medialibrary.getMedia(location)
-            if (mediaWrapper === null) {
-                if (!location.validateLocation()) {
-                    Log.w(TAG, "Invalid location $location")
-                    service.showToast(service.resources.getString(R.string.invalid_location, location), Toast.LENGTH_SHORT)
-                    continue
+        launch {
+            val mediaList = ArrayList<MediaWrapper>()
+            withContext(Dispatchers.IO) {
+                for (location in mediaPathList) {
+                    var mediaWrapper = medialibrary.getMedia(location)
+                    if (mediaWrapper === null) {
+                        if (!location.validateLocation()) {
+                            Log.w(TAG, "Invalid location $location")
+                            service.showToast(service.resources.getString(R.string.invalid_location, location), Toast.LENGTH_SHORT)
+                            continue
+                        }
+                        Log.v(TAG, "Creating on-the-fly Media object for $location")
+                        mediaWrapper = MediaWrapper(Uri.parse(location))
+                    }
+                    mediaList.add(mediaWrapper)
                 }
-                Log.v(TAG, "Creating on-the-fly Media object for $location")
-                mediaWrapper = MediaWrapper(Uri.parse(location))
             }
-            mediaList.add(mediaWrapper)
+            load(mediaList, position)
         }
-        load(mediaList, position)
     }
 
     @MainThread
@@ -278,7 +281,7 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
         if (isBenchmark) mw.addFlags(MediaWrapper.MEDIA_BENCHMARK)
         parsed = false
         player.switchToVideo = false
-        if (TextUtils.equals(mw.uri.scheme, "content")) MediaUtils.retrieveMediaTitle(mw)
+        if (TextUtils.equals(mw.uri.scheme, "content")) withContext(Dispatchers.IO) { MediaUtils.retrieveMediaTitle(mw) }
 
         if (mw.hasFlag(MediaWrapper.MEDIA_FORCE_AUDIO) && player.getAudioTracksCount() == 0) {
             determinePrevAndNextIndices(true)
@@ -317,11 +320,10 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
                         id = internalMedia.id
                     else {
                         internalMedia = if (mw.type == MediaWrapper.TYPE_STREAM) medialibrary.addStream(Uri.decode(mw.uri.toString()), mw.title) else medialibrary.addMedia(Uri.decode(mw.uri.toString()))
-                        if (internalMedia != null)
-                            id = internalMedia.id
+                        if (internalMedia != null) id = internalMedia.id
                     }
                 }
-                medialibrary.increasePlayCount(id)
+                if (id != 0L) medialibrary.increasePlayCount(id)
             }
             saveCurrentMedia()
         } else { //Start VideoPlayer for first video, it will trigger playIndex when ready.



More information about the Android mailing list