[Android] Pass context to PlayerController

Geoffrey Métais git at videolan.org
Fri Aug 10 15:09:37 CEST 2018


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Fri Aug 10 14:43:09 2018 +0200| [7463d92d3ade0f7fa6c6977de8a7a54355e4382e] | committer: Geoffrey Métais

Pass context to PlayerController

Avoid getting app context from static field

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

 .../src/org/videolan/vlc/PreviewVideoInputService.kt |  2 +-
 .../src/org/videolan/vlc/media/PlayerController.kt   | 20 ++++++++++----------
 .../src/org/videolan/vlc/media/PlaylistManager.kt    |  2 +-
 3 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/PreviewVideoInputService.kt b/vlc-android/src/org/videolan/vlc/PreviewVideoInputService.kt
index 6b124ff16..0ed8702b6 100644
--- a/vlc-android/src/org/videolan/vlc/PreviewVideoInputService.kt
+++ b/vlc-android/src/org/videolan/vlc/PreviewVideoInputService.kt
@@ -34,7 +34,7 @@ class PreviewVideoInputService : TvInputService() {
     private inner class PreviewSession(private val context: Context
     ) : TvInputService.Session(context), MediaPlayerEventListener {
 
-        val player by lazy(LazyThreadSafetyMode.NONE) { PlayerController() }
+        val player by lazy(LazyThreadSafetyMode.NONE) { PlayerController(applicationContext) }
 
         override fun onRelease() {
             player.release()
diff --git a/vlc-android/src/org/videolan/vlc/media/PlayerController.kt b/vlc-android/src/org/videolan/vlc/media/PlayerController.kt
index 6a61b5fb1..7f66c0317 100644
--- a/vlc-android/src/org/videolan/vlc/media/PlayerController.kt
+++ b/vlc-android/src/org/videolan/vlc/media/PlayerController.kt
@@ -1,6 +1,7 @@
 package org.videolan.vlc.media
 
 import android.arch.lifecycle.MutableLiveData
+import android.content.Context
 import android.net.Uri
 import android.support.annotation.MainThread
 import android.support.v4.media.session.PlaybackStateCompat
@@ -13,22 +14,21 @@ import org.videolan.libvlc.*
 import org.videolan.medialibrary.media.MediaWrapper
 import org.videolan.vlc.BuildConfig
 import org.videolan.vlc.RendererDelegate
-import org.videolan.vlc.VLCApplication
-import org.videolan.vlc.database.MediaDatabase
 import org.videolan.vlc.gui.preferences.PreferencesActivity
 import org.videolan.vlc.repository.SlaveRepository
+import org.videolan.vlc.util.Settings
 import org.videolan.vlc.util.VLCInstance
 import org.videolan.vlc.util.VLCOptions
 import kotlin.math.abs
 
 @Suppress("EXPERIMENTAL_FEATURE_WARNING")
-class PlayerController : IVLCVout.Callback, MediaPlayer.EventListener {
+class PlayerController(val context: Context) : IVLCVout.Callback, MediaPlayer.EventListener {
 
 //    private val exceptionHandler by lazy(LazyThreadSafetyMode.NONE) { CoroutineExceptionHandler { _, _ -> onPlayerError() } }
     private val playerContext by lazy(LazyThreadSafetyMode.NONE) { newSingleThreadContext("vlc-player") }
-    private val settings by lazy(LazyThreadSafetyMode.NONE) { VLCApplication.getSettings() }
+    private val settings by lazy(LazyThreadSafetyMode.NONE) { Settings.getInstance(context) }
     val progress by lazy(LazyThreadSafetyMode.NONE) { MutableLiveData<Progress>().apply { value = Progress() } }
-    private val slaveRepository by lazy { SlaveRepository.getInstance(VLCApplication.getAppContext()) }
+    private val slaveRepository by lazy { SlaveRepository.getInstance(context) }
 
     private var mediaplayer = newMediaPlayer()
     var switchToVideo = false
@@ -76,7 +76,7 @@ class PlayerController : IVLCVout.Callback, MediaPlayer.EventListener {
         mediaplayer.setEventListener(null)
         mediaplayer.media = media.apply { if (hasRenderer) parse() }
         mediaplayer.setEventListener(this at PlayerController)
-        mediaplayer.setEqualizer(VLCOptions.getEqualizerSetFromSettings(VLCApplication.getAppContext()))
+        mediaplayer.setEqualizer(VLCOptions.getEqualizerSetFromSettings(context))
         mediaplayer.setVideoTitleDisplay(MediaPlayer.Position.Disable, 0)
         mediaplayer.play()
         if (mediaplayer.rate == 1.0f && settings.getBoolean(PreferencesActivity.KEY_PLAYBACK_SPEED_PERSIST, true))
@@ -185,7 +185,7 @@ class PlayerController : IVLCVout.Callback, MediaPlayer.EventListener {
                 try {
                     withTimeout(5000, { player.release() })
                 } catch (exception: TimeoutCancellationException) {
-                    launch(UI.immediate) { Toast.makeText(VLCApplication.getAppContext(), "media stop has timeouted!", Toast.LENGTH_LONG).show() }
+                    launch(UI.immediate) { Toast.makeText(context, "media stop has timeouted!", Toast.LENGTH_LONG).show() }
                 }
             } else player.release()
         }
@@ -208,8 +208,8 @@ class PlayerController : IVLCVout.Callback, MediaPlayer.EventListener {
 
     private fun newMediaPlayer() : MediaPlayer {
         return MediaPlayer(VLCInstance.get()).apply {
-            setAudioDigitalOutputEnabled(VLCOptions.isAudioDigitalOutputEnabled(VLCApplication.getSettings()));
-            VLCOptions.getAout(VLCApplication.getSettings())?.let { setAudioOutput(it) }
+            setAudioDigitalOutputEnabled(VLCOptions.isAudioDigitalOutputEnabled(settings))
+            VLCOptions.getAout(settings)?.let { setAudioOutput(it) }
             setRenderer(RendererDelegate.selectedRenderer.value)
             this.vlcVout.addCallback(this at PlayerController)
         }
@@ -324,7 +324,7 @@ class PlayerController : IVLCVout.Callback, MediaPlayer.EventListener {
 //    private fun onPlayerError() {
 //        launch(UI) {
 //            restart()
-//            Toast.makeText(VLCApplication.getAppContext(), VLCApplication.getAppContext().getString(R.string.feedback_player_crashed), Toast.LENGTH_LONG).show()
+//            Toast.makeText(context, context.getString(R.string.feedback_player_crashed), Toast.LENGTH_LONG).show()
 //        }
 //    }
 }
diff --git a/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt b/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
index 6e5a5d9b6..6f7006b50 100644
--- a/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
+++ b/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
@@ -43,7 +43,7 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
     }
 
     private val medialibrary by lazy(LazyThreadSafetyMode.NONE) { Medialibrary.getInstance() }
-    val player by lazy(LazyThreadSafetyMode.NONE) { PlayerController() }
+    val player by lazy(LazyThreadSafetyMode.NONE) { PlayerController(service.applicationContext) }
     private val settings by lazy(LazyThreadSafetyMode.NONE) { PreferenceManager.getDefaultSharedPreferences(service) }
     private val ctx by lazy(LazyThreadSafetyMode.NONE) { VLCApplication.getAppContext() }
     var currentIndex = -1



More information about the Android mailing list