[Android] Update track length if unknown at the start of playback

Robert Stone git at videolan.org
Wed Nov 25 10:21:25 CET 2020


vlc-android | branch: master | Robert Stone <rhstone at gmail.com> | Tue Nov 24 16:35:36 2020 -0800| [961451421a5760af6d1ea0144ee78870d2be6245] | committer: Nicolas Pomepuy

Update track length if unknown at the start of playback

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

 .../vlc-android/src/org/videolan/vlc/PlaybackService.kt      | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt b/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
index fc9383f4f..1a1ef9a70 100644
--- a/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
+++ b/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
@@ -115,6 +115,7 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner {
     private var notificationShowing = false
     private var prevUpdateInCarMode = true
     private var lastTime = 0L
+    private var lastLength = 0L
     private var widget = 0
     /**
      * Last widget position update timestamp
@@ -189,6 +190,12 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner {
                 if (wakeLock.isHeld) wakeLock.release()
             }
             MediaPlayer.Event.EncounteredError -> executeUpdate()
+            MediaPlayer.Event.LengthChanged -> {
+                if (lastLength == 0L) {
+                    executeUpdate()
+                    publishState()
+                }
+            }
             MediaPlayer.Event.PositionChanged -> {
                 if (time < 1000L && time < lastTime) publishState()
                 lastTime = time
@@ -842,6 +849,7 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner {
         if (!this::mediaSession.isInitialized) initMediaSession()
         val ctx = this
         val length = length
+        lastLength = length
         val bob = withContext(Dispatchers.Default) {
             val title = media.nowPlaying ?: media.title
             val coverOnLockscreen = settings.getBoolean("lockscreen_cover", true)
@@ -870,9 +878,9 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner {
             }
             bob.putLong("shuffle", 1L)
             bob.putLong("repeat", repeatType.toLong())
-            return at withContext bob
+            return at withContext bob.build()
         }
-        if (this at PlaybackService::mediaSession.isInitialized) mediaSession.setMetadata(bob.build())
+        if (this at PlaybackService::mediaSession.isInitialized) mediaSession.setMetadata(bob)
     }
 
     private fun publishState(position: Long? = null) {



More information about the Android mailing list