[Android] Player: set state in a compamion object

Geoffrey Métais git at videolan.org
Wed Oct 30 18:25:21 CET 2019


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Tue Oct 29 18:06:13 2019 +0100| [51fb6ba1cf3d9b8b2c057faaf31344aa5998adaf] | committer: Geoffrey Métais

Player: set state in a compamion object

Avoid loading a player when we check playback state

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

 vlc-android/src/org/videolan/vlc/PlaybackService.kt            |  5 +++--
 .../src/org/videolan/vlc/gui/helpers/PlayerOptionsDelegate.kt  |  3 ++-
 vlc-android/src/org/videolan/vlc/media/PlayerController.kt     | 10 ++++++----
 vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt      |  2 +-
 4 files changed, 12 insertions(+), 8 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/PlaybackService.kt b/vlc-android/src/org/videolan/vlc/PlaybackService.kt
index 18da930af..43d0bebda 100644
--- a/vlc-android/src/org/videolan/vlc/PlaybackService.kt
+++ b/vlc-android/src/org/videolan/vlc/PlaybackService.kt
@@ -62,6 +62,7 @@ import org.videolan.vlc.gui.video.PopupManager
 import org.videolan.vlc.gui.video.VideoPlayerActivity
 import org.videolan.vlc.media.MediaSessionBrowser
 import org.videolan.vlc.media.MediaUtils
+import org.videolan.vlc.media.PlayerController
 import org.videolan.vlc.media.PlaylistManager
 import org.videolan.vlc.util.*
 import org.videolan.vlc.widget.VLCAppWidgetProvider
@@ -566,7 +567,7 @@ class PlaybackService : MediaBrowserServiceCompat(), CoroutineScope, LifecycleOw
     private fun forceForeground() {
         val ctx = this at PlaybackService
         NotificationHelper.createNotificationChannels(ctx.applicationContext)
-        val stopped = playlistManager.player.playbackState == PlaybackStateCompat.STATE_STOPPED
+        val stopped = PlayerController.playbackState == PlaybackStateCompat.STATE_STOPPED
         val notification = if (this::notification.isInitialized && !stopped) notification
         else NotificationHelper.createPlaybackNotification(ctx, false,
                 ctx.resources.getString(R.string.loading), "", "", null,
@@ -816,7 +817,7 @@ class PlaybackService : MediaBrowserServiceCompat(), CoroutineScope, LifecycleOw
         var actions = PLAYBACK_BASE_ACTIONS
         val hasMedia = playlistManager.hasCurrentMedia()
         var time = position ?: time
-        var state = playlistManager.player.playbackState
+        var state = PlayerController.playbackState
         when (state) {
             PlaybackStateCompat.STATE_PLAYING -> actions = actions or (PlaybackStateCompat.ACTION_PAUSE or PlaybackStateCompat.ACTION_STOP)
             PlaybackStateCompat.STATE_PAUSED -> actions = actions or (PlaybackStateCompat.ACTION_PLAY or PlaybackStateCompat.ACTION_STOP)
diff --git a/vlc-android/src/org/videolan/vlc/gui/helpers/PlayerOptionsDelegate.kt b/vlc-android/src/org/videolan/vlc/gui/helpers/PlayerOptionsDelegate.kt
index 4ec3b6aaa..73491bb49 100644
--- a/vlc-android/src/org/videolan/vlc/gui/helpers/PlayerOptionsDelegate.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/helpers/PlayerOptionsDelegate.kt
@@ -33,6 +33,7 @@ import org.videolan.vlc.gui.dialogs.*
 import org.videolan.vlc.gui.video.VideoPlayerActivity
 import org.videolan.vlc.interfaces.IPlaybackSettingsController
 import org.videolan.vlc.media.ABRepeat
+import org.videolan.vlc.media.PlayerController
 import org.videolan.vlc.util.*
 import java.util.*
 
@@ -91,7 +92,7 @@ class PlayerOptionsDelegate(val activity: AppCompatActivity, val service: Playba
     }
 
     fun setup() {
-        if (!this::recyclerview.isInitialized || service.playlistManager.player.playbackState == PlaybackStateCompat.STATE_STOPPED) return
+        if (!this::recyclerview.isInitialized || PlayerController.playbackState == PlaybackStateCompat.STATE_STOPPED) return
         val options = mutableListOf<PlayerOption>()
         when (playerOptionType) {
             PlayerOptionType.ADVANCED -> {
diff --git a/vlc-android/src/org/videolan/vlc/media/PlayerController.kt b/vlc-android/src/org/videolan/vlc/media/PlayerController.kt
index 39dd69859..c48084832 100644
--- a/vlc-android/src/org/videolan/vlc/media/PlayerController.kt
+++ b/vlc-android/src/org/videolan/vlc/media/PlayerController.kt
@@ -36,8 +36,6 @@ class PlayerController(val context: Context) : IVLCVout.Callback, MediaPlayer.Ev
     var pausable = false
     var previousMediaStats: Media.Stats? = null
         private set
-    @Volatile var playbackState = PlaybackStateCompat.STATE_STOPPED
-        private set
     @Volatile var hasRenderer = false
         private set
 
@@ -264,7 +262,7 @@ class PlayerController(val context: Context) : IVLCVout.Callback, MediaPlayer.Ev
     fun getTitleIdx() = if (!mediaplayer.isReleased) mediaplayer.title else -1
 
     fun setTitleIdx(title: Int) {
-       if (!mediaplayer.isReleased)  mediaplayer.title = title
+        if (!mediaplayer.isReleased)  mediaplayer.title = title
     }
 
     fun getVolume() = if (!mediaplayer.isReleased) mediaplayer.volume else 100
@@ -320,12 +318,16 @@ class PlayerController(val context: Context) : IVLCVout.Callback, MediaPlayer.Ev
         lastTime = 0L
     }
 
-//    private fun onPlayerError() {
+    //    private fun onPlayerError() {
 //        launch(UI) {
 //            restart()
 //            Toast.makeText(context, context.getString(R.string.feedback_player_crashed), Toast.LENGTH_LONG).show()
 //        }
 //    }
+    companion object {
+        @Volatile var playbackState = PlaybackStateCompat.STATE_STOPPED
+            private set
+    }
 }
 
 class Progress(var time: Long = 0L, var length: Long = 0L)
diff --git a/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt b/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
index a89463acd..edf44460d 100644
--- a/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
+++ b/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
@@ -262,7 +262,7 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
 
     fun setRenderer(item: RendererItem?) {
         player.setRenderer(item)
-        showAudioPlayer.value = player.playbackState != PlaybackStateCompat.STATE_STOPPED && (item !== null || !player.isVideoPlaying())
+        showAudioPlayer.value = PlayerController.playbackState != PlaybackStateCompat.STATE_STOPPED && (item !== null || !player.isVideoPlaying())
     }
 
     suspend fun playIndex(index: Int, flags: Int = 0) {



More information about the Android mailing list