Commit d90955e2 authored by Geoffrey Métais's avatar Geoffrey Métais
Browse files

Load ML if needed to get media from ID

parent 238a9bfe
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"
@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@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@PreviewVideoInputService.startMedialibrary(false, false, false)
}
}
private var width = 0
private var height = 0
private lateinit var surface: Surface
......
......@@ -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();
......
......@@ -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) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment