[Android] Workaround to seek in video with no length

Nicolas Pomepuy git at videolan.org
Fri Jun 25 04:49:11 UTC 2021


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Wed Jun 23 10:07:58 2021 +0200| [f71e587bc0d16ed633bcb05972d714a08e15c854] | committer: Nicolas Pomepuy

Workaround to seek in video with no length

Fixes #643

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

 .../vlc-android/src/org/videolan/vlc/PlaybackService.kt      | 12 +++++-------
 .../src/org/videolan/vlc/gui/video/VideoPlayerActivity.kt    |  3 ++-
 .../src/org/videolan/vlc/media/PlayerController.kt           |  5 +++--
 3 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt b/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
index 7bc4d3059..017026e84 100644
--- a/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
+++ b/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
@@ -74,10 +74,7 @@ import org.videolan.vlc.gui.helpers.NotificationHelper
 import org.videolan.vlc.gui.helpers.getBitmapFromDrawable
 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.media.*
 import org.videolan.vlc.util.*
 import org.videolan.vlc.widget.VLCAppWidgetProvider
 import org.videolan.vlc.widget.VLCAppWidgetProviderBlack
@@ -90,6 +87,7 @@ private const val TAG = "VLC/PlaybackService"
 @ExperimentalCoroutinesApi
 @ObsoleteCoroutinesApi
 class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner {
+    private var position: Long = -1L
     private val dispatcher = ServiceLifecycleDispatcher(this)
 
     internal var enabledActions = PLAYBACK_BASE_ACTIONS
@@ -197,6 +195,7 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner {
                 }
             }
             MediaPlayer.Event.PositionChanged -> {
+                if (length == 0L) position = (NO_LENGTH_PROGRESS_MAX.toLong() * event.positionChanged).toLong()
                 if (time < 1000L && time < lastTime) publishState()
                 lastTime = time
                 if (widget != 0) updateWidgetPosition(event.positionChanged)
@@ -1341,8 +1340,7 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner {
     @MainThread
     @JvmOverloads
     fun seek(position: Long, length: Double = this.length.toDouble(), fromUser: Boolean = false) {
-        if (length > 0.0) setPosition((position / length).toFloat())
-        else time = position
+        if (length > 0.0) setPosition((position / length).toFloat()) else setPosition((position.toFloat() / NO_LENGTH_PROGRESS_MAX.toFloat()))
         if (fromUser) {
             publishState(position)
         }
@@ -1503,7 +1501,7 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner {
             }
         }
 
-        return realTime.toInt()
+        return if (length == 0L) position.toInt() else realTime.toInt()
     }
 }
 
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.kt b/application/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.kt
index cdfcb63ad..cbeaef3b2 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.kt
@@ -101,6 +101,7 @@ import org.videolan.vlc.gui.helpers.PlayerOptionsDelegate
 import org.videolan.vlc.gui.helpers.UiTools
 import org.videolan.vlc.gui.helpers.hf.StoragePermissionsDelegate
 import org.videolan.vlc.interfaces.IPlaybackSettingsController
+import org.videolan.vlc.media.NO_LENGTH_PROGRESS_MAX
 import org.videolan.vlc.repository.ExternalSubRepository
 import org.videolan.vlc.repository.SlaveRepository
 import org.videolan.vlc.util.*
@@ -2169,5 +2170,5 @@ fun setConstraintPercent(view: Guideline, percent: Float) {
 
 @BindingAdapter("mediamax")
 fun setProgressMax(view: SeekBar, length: Long) {
-    view.max = length.toInt()
+    view.max =  if (length == 0L) NO_LENGTH_PROGRESS_MAX else length.toInt()
 }
\ No newline at end of file
diff --git a/application/vlc-android/src/org/videolan/vlc/media/PlayerController.kt b/application/vlc-android/src/org/videolan/vlc/media/PlayerController.kt
index ed0af17e7..4b5137a65 100644
--- a/application/vlc-android/src/org/videolan/vlc/media/PlayerController.kt
+++ b/application/vlc-android/src/org/videolan/vlc/media/PlayerController.kt
@@ -109,8 +109,8 @@ class PlayerController(val context: Context) : IVLCVout.Callback, MediaPlayer.Ev
     }
 
     fun seek(position: Long, length: Double = getLength().toDouble()) {
-        if (length > 0.0) setPosition((position / length).toFloat())
-        else setTime(position)
+        val pos = if (length > 0.0) (position / length).toFloat() else (position / NO_LENGTH_PROGRESS_MAX).toFloat()
+        setPosition(pos)
     }
 
     fun setPosition(position: Float) {
@@ -355,6 +355,7 @@ class PlayerController(val context: Context) : IVLCVout.Callback, MediaPlayer.Ev
     }
 }
 
+const val NO_LENGTH_PROGRESS_MAX = 1000
 class Progress(var time: Long = 0L, var length: Long = 0L)
 
 internal interface MediaPlayerEventListener {



More information about the Android mailing list