[Android] Provide context when launching LibVLC

Geoffrey Métais git at videolan.org
Tue Mar 19 15:35:15 CET 2019


vlc-android | branch: 3.1.x | Geoffrey Métais <geoffrey.metais at gmail.com> | Mon Mar 18 11:45:42 2019 +0100| [20e2bd1a3103258cad42e7d23e09883fe4796000] | committer: Geoffrey Métais

Provide context when launching LibVLC

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

 .../src/org/videolan/vlc/PreviewVideoInputService.kt      |  2 +-
 vlc-android/src/org/videolan/vlc/RendererDelegate.kt      |  4 +++-
 vlc-android/src/org/videolan/vlc/VLCApplication.java      |  2 +-
 vlc-android/src/org/videolan/vlc/gui/InfoActivity.java    | 15 +++++++--------
 .../src/org/videolan/vlc/media/PlayerController.kt        |  2 +-
 vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt |  2 +-
 .../src/org/videolan/vlc/providers/BrowserProvider.kt     |  2 +-
 vlc-android/src/org/videolan/vlc/util/VLCInstance.java    |  7 ++++---
 8 files changed, 19 insertions(+), 17 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/PreviewVideoInputService.kt b/vlc-android/src/org/videolan/vlc/PreviewVideoInputService.kt
index 840755492..161535352 100644
--- a/vlc-android/src/org/videolan/vlc/PreviewVideoInputService.kt
+++ b/vlc-android/src/org/videolan/vlc/PreviewVideoInputService.kt
@@ -52,7 +52,7 @@ class PreviewVideoInputService : TvInputService(), CoroutineScope {
                     return at launch
                 }
                 try {
-                    val media = Media(VLCInstance.get(), mw.uri)
+                    val media = Media(VLCInstance.get(this at PreviewVideoInputService), mw.uri)
                     val start = if (mw.length <= 0L) 0 else mw.length.random()/1000
                     media.addOption(":start-time=$start")
                     player.getVout()?.apply {
diff --git a/vlc-android/src/org/videolan/vlc/RendererDelegate.kt b/vlc-android/src/org/videolan/vlc/RendererDelegate.kt
index 4df9f323f..c0b1c12d7 100644
--- a/vlc-android/src/org/videolan/vlc/RendererDelegate.kt
+++ b/vlc-android/src/org/videolan/vlc/RendererDelegate.kt
@@ -41,8 +41,10 @@ object RendererDelegate : RendererDiscoverer.EventListener {
 
     suspend fun start() {
         if (started) return
+        val libVlc = VLCApplication.getAppContext()?.let {
+            withContext(Dispatchers.IO) { VLCInstance.get(it) }
+        } ?: return
         started = true
-        val libVlc = withContext(Dispatchers.IO) { VLCInstance.get() }
         for (discoverer in RendererDiscoverer.list(libVlc)) {
             val rd = RendererDiscoverer(libVlc, discoverer.name)
             discoverers.add(rd)
diff --git a/vlc-android/src/org/videolan/vlc/VLCApplication.java b/vlc-android/src/org/videolan/vlc/VLCApplication.java
index 8a13353d5..7c10ac8e5 100644
--- a/vlc-android/src/org/videolan/vlc/VLCApplication.java
+++ b/vlc-android/src/org/videolan/vlc/VLCApplication.java
@@ -103,7 +103,7 @@ public class VLCApplication extends Application {
                         AudioUtil.prepareCacheFolder(getAppContext());
 
                         if (!VLCInstance.testCompatibleCPU(getAppContext())) return;
-                        Dialog.setCallbacks(VLCInstance.get(), mDialogCallbacks);
+                        Dialog.setCallbacks(VLCInstance.get(instance), mDialogCallbacks);
                     }
                 });
             }
diff --git a/vlc-android/src/org/videolan/vlc/gui/InfoActivity.java b/vlc-android/src/org/videolan/vlc/gui/InfoActivity.java
index 34ad507dd..c769a5d49 100644
--- a/vlc-android/src/org/videolan/vlc/gui/InfoActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/InfoActivity.java
@@ -1,6 +1,7 @@
 package org.videolan.vlc.gui;
 
 
+import android.content.Context;
 import android.graphics.Bitmap;
 import android.graphics.drawable.BitmapDrawable;
 import android.net.Uri;
@@ -270,15 +271,15 @@ public class InfoActivity extends AudioPlayerContainerActivity implements View.O
     }
 
     private class ParseTracksTask extends AsyncTask<Void, Void, Media> {
+        final Context context = getApplicationContext();
 
         @Override
         protected Media doInBackground(Void... params) {
 
-            final LibVLC libVlc = VLCInstance.get();
-            if (libVlc == null || isCancelled())
-                return null;
+            final LibVLC libVlc = VLCInstance.get(context);
+            if (libVlc == null || isCancelled()) return null;
 
-            Media media = new Media(libVlc, ((MediaWrapper)mItem).getUri());
+            final Media media = new Media(libVlc, ((MediaWrapper)mItem).getUri());
             media.parse();
 
             return media;
@@ -287,8 +288,7 @@ public class InfoActivity extends AudioPlayerContainerActivity implements View.O
         @Override
         protected void onPostExecute(Media media) {
             mParseTracksTask = null;
-            if (media == null || isCancelled())
-                return;
+            if (media == null || isCancelled()) return;
             boolean hasSubs = false;
             final int trackCount = media.getTrackCount();
             List<Media.Track> tracks = new LinkedList<>();
@@ -299,8 +299,7 @@ public class InfoActivity extends AudioPlayerContainerActivity implements View.O
             }
             media.release();
             mAdapter.setTracks(tracks);
-            if (hasSubs)
-                mBinding.infoSubtitles.setVisibility(View.VISIBLE);
+            if (hasSubs) mBinding.infoSubtitles.setVisibility(View.VISIBLE);
         }
 
         @Override
diff --git a/vlc-android/src/org/videolan/vlc/media/PlayerController.kt b/vlc-android/src/org/videolan/vlc/media/PlayerController.kt
index 5044d41a0..5d96abba4 100644
--- a/vlc-android/src/org/videolan/vlc/media/PlayerController.kt
+++ b/vlc-android/src/org/videolan/vlc/media/PlayerController.kt
@@ -207,7 +207,7 @@ class PlayerController(val context: Context) : IVLCVout.Callback, MediaPlayer.Ev
     }
 
     private fun newMediaPlayer() : MediaPlayer {
-        return MediaPlayer(VLCInstance.get()).apply {
+        return MediaPlayer(VLCInstance.get(context)).apply {
             setAudioDigitalOutputEnabled(VLCOptions.isAudioDigitalOutputEnabled(settings))
             VLCOptions.getAout(settings)?.let { setAudioOutput(it) }
             setRenderer(PlaybackService.renderer.value)
diff --git a/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt b/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
index 7ceb814fe..e9ed30069 100644
--- a/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
+++ b/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
@@ -293,7 +293,7 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
                 return
             }
             val start = getStartTime(mw)
-            val media = Media(VLCInstance.get(), uri)
+            val media = Media(VLCInstance.get(service), uri)
             media.addOption(":start-time=$start")
             VLCOptions.setMediaOptions(media, ctx, flags or mw.flags)
             /* keeping only video during benchmark */
diff --git a/vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt b/vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt
index 5878d9742..1f31183d4 100644
--- a/vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt
+++ b/vlc-android/src/org/videolan/vlc/providers/BrowserProvider.kt
@@ -77,7 +77,7 @@ abstract class BrowserProvider(val context: Context, val dataset: LiveDataset<Me
     }
 
     protected open fun initBrowser() {
-        if (mediabrowser == null) mediabrowser = MediaBrowser(VLCInstance.get(), this, browserHandler)
+        if (mediabrowser == null) mediabrowser = MediaBrowser(VLCInstance.get(context), this, browserHandler)
     }
 
     open fun fetch() {
diff --git a/vlc-android/src/org/videolan/vlc/util/VLCInstance.java b/vlc-android/src/org/videolan/vlc/util/VLCInstance.java
index 5188c06df..26be0562c 100644
--- a/vlc-android/src/org/videolan/vlc/util/VLCInstance.java
+++ b/vlc-android/src/org/videolan/vlc/util/VLCInstance.java
@@ -20,10 +20,10 @@
 
 package org.videolan.vlc.util;
 
+import android.annotation.SuppressLint;
 import android.app.Activity;
 import android.content.Context;
 import android.content.Intent;
-import android.content.res.AssetManager;
 import android.util.Log;
 
 import org.videolan.libvlc.LibVLC;
@@ -35,14 +35,15 @@ import org.videolan.vlc.gui.CompatErrorActivity;
 public class VLCInstance {
     public final static String TAG = "VLC/UiTools/VLCInstance";
 
+    @SuppressLint("StaticFieldLeak")
     private static LibVLC sLibVLC = null;
 
     /** A set of utility functions for the VLC application */
-    public synchronized static LibVLC get() throws IllegalStateException {
+    public synchronized static LibVLC get(Context ctx) throws IllegalStateException {
         if (sLibVLC == null) {
             Thread.setDefaultUncaughtExceptionHandler(new VLCCrashHandler());
 
-            final Context context = VLCApplication.getAppContext();
+            final Context context = ctx.getApplicationContext();
             if(!VLCUtil.hasCompatibleCPU(context)) {
                 Log.e(TAG, VLCUtil.getErrorMsg());
                 throw new IllegalStateException("LibVLC initialisation failed: " + VLCUtil.getErrorMsg());



More information about the Android mailing list