[Android] Remove I/O calls from main thread

Geoffrey Métais git at videolan.org
Tue Jan 8 17:48:10 CET 2019


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Tue Jan  8 17:47:44 2019 +0100| [bd19e61f2ffd91499bc5fc79e5b2f1d0d1d887b0] | committer: Geoffrey Métais

Remove I/O calls from main thread

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

 .../src/org/videolan/vlc/media/PlaylistManager.kt  | 47 +++++++++++++---------
 1 file changed, 29 insertions(+), 18 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt b/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
index 877e7808d..821510491 100644
--- a/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
+++ b/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
@@ -400,22 +400,26 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
     }
 
     fun saveMediaMeta() {
-        val media = medialibrary.findMedia(getCurrentMedia())
-        if (media === null || media.id == 0L) return
+        val currentMedia = getCurrentMedia() ?: return
+        if (currentMedia.uri.scheme == "fd") return
+        //Save progress
+        val time = player.getCurrentTime()
+        val length = player.getLength()
         val canSwitchToVideo = player.canSwitchToVideo()
-        if (media.type == MediaWrapper.TYPE_VIDEO || canSwitchToVideo || media.isPodcast) {
-            //Save progress
-            val time = player.getCurrentTime()
-            val length = player.getLength()
-            var progress = time / length.toFloat()
-            if (progress > 0.95f || length - time < 10000) {
-                //increase seen counter if more than 95% of the media have been seen
-                //and reset progress to 0
-                media.setLongMeta(MediaWrapper.META_SEEN, ++media.seen)
-                progress = 0f
+        launch {
+            val media = withContext(Dispatchers.IO) { medialibrary.findMedia(currentMedia) }
+            if (media === null || media.id == 0L) return at launch
+            if (media.type == MediaWrapper.TYPE_VIDEO || canSwitchToVideo || media.isPodcast) {
+                var progress = time / length.toFloat()
+                if (progress > 0.95f || length - time < 10000) {
+                    //increase seen counter if more than 95% of the media have been seen
+                    //and reset progress to 0
+                    launch(Dispatchers.IO) { media.setLongMeta(MediaWrapper.META_SEEN, ++media.seen) }
+                    progress = 0f
+                }
+                media.time = if (progress == 0f) 0L else time
+                launch(Dispatchers.IO) { media.setLongMeta(MediaWrapper.META_PROGRESS, media.time) }
             }
-            media.time = if (progress == 0f) 0L else time
-            media.setLongMeta(MediaWrapper.META_PROGRESS, media.time)
         }
     }
 
@@ -575,10 +579,17 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
                 child.release()
             }
             if (mrl !== null && ml.count == 1) {
-                if (stream) {
-                    getCurrentMedia()!!.type = MediaWrapper.TYPE_STREAM
-                    medialibrary.addStream(mrl, getCurrentMedia()!!.title)
-                } else medialibrary.addToHistory(mrl, getCurrentMedia()!!.title)
+                getCurrentMedia()?.apply {
+                    launch(Dispatchers.IO) {
+                        if (stream) {
+                            type = MediaWrapper.TYPE_STREAM
+                            medialibrary.addStream(mrl, title)
+                        } else if (uri.scheme != "fd") {
+                            medialibrary.addToHistory(mrl, title)
+                        }
+                    }
+                }
+
             }
             ret = index
         }



More information about the Android mailing list