[Android] Load ML if needed to get media from ID

Geoffrey Métais git at videolan.org
Tue Jul 24 17:00:55 CEST 2018


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Mon Jul 23 17:11:32 2018 +0200| [d90955e2eb285eb2cf6c2f9a1aeb2283406515b2] | committer: Geoffrey Métais

Load ML if needed to get media from ID

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

 .../org/videolan/vlc/PreviewVideoInputService.kt   | 26 +++++-----------------
 .../src/org/videolan/vlc/StartActivity.java        |  2 +-
 .../src/org/videolan/vlc/media/MediaUtils.kt       |  8 +++----
 3 files changed, 9 insertions(+), 27 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/PreviewVideoInputService.kt b/vlc-android/src/org/videolan/vlc/PreviewVideoInputService.kt
index de7c57bd8..6b124ff16 100644
--- a/vlc-android/src/org/videolan/vlc/PreviewVideoInputService.kt
+++ b/vlc-android/src/org/videolan/vlc/PreviewVideoInputService.kt
@@ -1,30 +1,30 @@
 package org.videolan.vlc
 
 
+import android.annotation.TargetApi
 import android.content.Context
 import android.media.tv.TvInputManager
 import android.media.tv.TvInputManager.VIDEO_UNAVAILABLE_REASON_TUNING
 import android.media.tv.TvInputService
 import android.net.Uri
+import android.os.Build
 import android.util.Log
 import android.view.Surface
 import kotlinx.coroutines.experimental.CoroutineStart
 import kotlinx.coroutines.experimental.android.UI
 import kotlinx.coroutines.experimental.launch
-import kotlinx.coroutines.experimental.withContext
 import org.videolan.libvlc.Media
 import org.videolan.libvlc.MediaPlayer
-import org.videolan.medialibrary.Medialibrary
 import org.videolan.vlc.media.MediaPlayerEventListener
 import org.videolan.vlc.media.PlayerController
-import org.videolan.vlc.util.VLCIO
 import org.videolan.vlc.util.VLCInstance
+import org.videolan.vlc.util.getFromMl
 import org.videolan.vlc.util.random
 import java.io.IOException
-import kotlin.coroutines.experimental.suspendCoroutine
 
 private const val TAG = "PreviewInputService"
 
+ at TargetApi(Build.VERSION_CODES.LOLLIPOP)
 class PreviewVideoInputService : TvInputService() {
 
     override fun onCreateSession(inputId: String): TvInputService.Session? {
@@ -44,7 +44,7 @@ class PreviewVideoInputService : TvInputService() {
             notifyVideoUnavailable(VIDEO_UNAVAILABLE_REASON_TUNING)
             val id = uri.lastPathSegment.toLong()
             launch(UI, CoroutineStart.UNDISPATCHED) {
-                val mw = getMedia(id)
+                val mw = this at PreviewVideoInputService.getFromMl { getMedia(id) }
                 if (mw == null) {
                     Log.w(TAG, "Could not find video $id")
                     notifyVideoUnavailable(TvInputManager.VIDEO_UNAVAILABLE_REASON_UNKNOWN)
@@ -71,22 +71,6 @@ class PreviewVideoInputService : TvInputService() {
             return true
         }
 
-        private suspend fun getMedia(id: Long) = withContext(VLCIO) {
-            val ml = Medialibrary.getInstance()
-            if (ml.isInitiated) ml.getMedia(id)
-            else suspendCoroutine { continuation ->
-                ml.addOnMedialibraryReadyListener(object : Medialibrary.OnMedialibraryReadyListener {
-                    override fun onMedialibraryReady() {
-                        ml.removeOnMedialibraryReadyListener(this)
-                        continuation.resume(ml.getMedia(id))
-                    }
-                    override fun onMedialibraryIdle() {}
-
-                })
-                this at PreviewVideoInputService.startMedialibrary(false, false, false)
-            }
-        }
-
         private var width = 0
         private var height = 0
         private lateinit var surface: Surface
diff --git a/vlc-android/src/org/videolan/vlc/StartActivity.java b/vlc-android/src/org/videolan/vlc/StartActivity.java
index 36dc6bab7..67f552968 100644
--- a/vlc-android/src/org/videolan/vlc/StartActivity.java
+++ b/vlc-android/src/org/videolan/vlc/StartActivity.java
@@ -99,7 +99,7 @@ public class StartActivity extends FragmentActivity implements StoragePermission
             if (TextUtils.equals(path,"/"+TvChannelUtilsKt.TV_CHANNEL_PATH_APP)) startApplication(tv, firstRun, upgrade);
             else if (TextUtils.equals(path,"/"+TvChannelUtilsKt.TV_CHANNEL_PATH_VIDEO)) {
                 final long id = Long.valueOf(data.getQueryParameter(TvChannelUtilsKt.TV_CHANNEL_QUERY_VIDEO_ID));
-                MediaUtils.INSTANCE.openMediaNoUi(id);
+                MediaUtils.INSTANCE.openMediaNoUi(this, id);
             }
         } else startApplication(tv, firstRun, upgrade);
         finish();
diff --git a/vlc-android/src/org/videolan/vlc/media/MediaUtils.kt b/vlc-android/src/org/videolan/vlc/media/MediaUtils.kt
index 46e0cbc0e..9f5673e70 100644
--- a/vlc-android/src/org/videolan/vlc/media/MediaUtils.kt
+++ b/vlc-android/src/org/videolan/vlc/media/MediaUtils.kt
@@ -15,9 +15,7 @@ import android.util.Log
 import kotlinx.coroutines.experimental.CoroutineStart
 import kotlinx.coroutines.experimental.android.UI
 import kotlinx.coroutines.experimental.launch
-import kotlinx.coroutines.experimental.withContext
 import org.videolan.libvlc.util.AndroidUtil
-import org.videolan.medialibrary.Medialibrary
 import org.videolan.medialibrary.Tools
 import org.videolan.medialibrary.media.MediaWrapper
 import org.videolan.medialibrary.media.Playlist
@@ -110,9 +108,9 @@ object MediaUtils {
         })
     }
 
-    fun openMediaNoUi(id: Long) = launch(UI, CoroutineStart.UNDISPATCHED) {
-        val media = withContext(VLCIO) {Medialibrary.getInstance().getMedia(id) }
-        openMediaNoUi(VLCApplication.getAppContext(), media)
+    fun openMediaNoUi(ctx: Context, id: Long) = launch(UI, CoroutineStart.UNDISPATCHED) {
+        val media = ctx.getFromMl { getMedia(id) }
+        openMediaNoUi(ctx, media)
     }
 
     fun openMediaNoUi(uri: Uri) {



More information about the Android mailing list