[Android] RendererDelegate: Load VLC instance in background

Geoffrey Métais git at videolan.org
Mon Feb 5 17:18:18 CET 2018


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Sat Feb  3 14:47:06 2018 +0100| [5ca88812ac3a600f367af1ee5bd0cf1cfa686e38] | committer: Geoffrey Métais

RendererDelegate: Load VLC instance in background

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

 vlc-android/src/org/videolan/vlc/RendererDelegate.kt | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/RendererDelegate.kt b/vlc-android/src/org/videolan/vlc/RendererDelegate.kt
index cb5ecc8e0..b6de114d8 100644
--- a/vlc-android/src/org/videolan/vlc/RendererDelegate.kt
+++ b/vlc-android/src/org/videolan/vlc/RendererDelegate.kt
@@ -19,6 +19,10 @@
  */
 package org.videolan.vlc
 
+import kotlinx.coroutines.experimental.CoroutineStart
+import kotlinx.coroutines.experimental.async
+import kotlinx.coroutines.experimental.launch
+import kotlinx.coroutines.experimental.android.UI
 import org.videolan.libvlc.RendererDiscoverer
 import org.videolan.libvlc.RendererItem
 import org.videolan.vlc.util.VLCInstance
@@ -48,19 +52,19 @@ object RendererDelegate : RendererDiscoverer.EventListener, ExternalMonitor.Netw
         fun onRendererChanged(renderer: RendererItem?)
     }
 
-    fun start() {
+    suspend fun start() {
         if (started) return
         started = true
-        val libVlc = VLCInstance.get()
+        val libVlc = async { VLCInstance.get() }.await()
         for (discoverer in RendererDiscoverer.list(libVlc)) {
             val rd = RendererDiscoverer(libVlc, discoverer.name)
             mDiscoverers.add(rd)
-            rd.setEventListener(this)
+            rd.setEventListener(this at RendererDelegate)
             rd.start()
         }
     }
 
-    fun stop() {
+    suspend fun stop() {
         if (!started) return
         started = false
         for (discoverer in mDiscoverers) discoverer.stop()
@@ -75,7 +79,9 @@ object RendererDelegate : RendererDiscoverer.EventListener, ExternalMonitor.Netw
         renderers.clear()
     }
 
-    override fun onNetworkConnectionChanged(connected: Boolean) = if (connected) start() else stop()
+    override fun onNetworkConnectionChanged(connected: Boolean) {
+        launch(UI, CoroutineStart.UNDISPATCHED) { if (connected) start() else stop() }
+    }
 
     override fun onEvent(event: RendererDiscoverer.Event?) {
         when (event?.type) {



More information about the Android mailing list