[Android] Cache player length

Geoffrey Métais git at videolan.org
Wed Feb 21 16:25:37 CET 2018


vlc-android | branch: 3.0.x | Geoffrey Métais <geoffrey.metais at gmail.com> | Sat Feb  3 19:04:31 2018 +0100| [cbda133cd93015ae54ac1c78584a460914690ee7] | committer: Geoffrey Métais

Cache player length

Signed-off-by: Geoffrey Métais <geoffrey.metais at gmail.com>

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

 .../src/org/videolan/vlc/media/PlayerController.kt | 23 +++++++++++++---------
 .../src/org/videolan/vlc/media/PlaylistManager.kt  |  6 +++---
 2 files changed, 17 insertions(+), 12 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/media/PlayerController.kt b/vlc-android/src/org/videolan/vlc/media/PlayerController.kt
index 2fe7f3d94..119e0a093 100644
--- a/vlc-android/src/org/videolan/vlc/media/PlayerController.kt
+++ b/vlc-android/src/org/videolan/vlc/media/PlayerController.kt
@@ -32,7 +32,8 @@ class PlayerController : IVLCVout.Callback, MediaPlayer.EventListener {
         private set
     @Volatile var hasRenderer = false
         private set
-    @Volatile var currentTime = 0L
+    @Volatile private var currentTime = 0L
+    @Volatile var length = 0L
         private set
 
     fun getVout(): IVLCVout? = mediaplayer.vlcVout
@@ -53,8 +54,7 @@ class PlayerController : IVLCVout.Callback, MediaPlayer.EventListener {
 
     fun stop() {
         if (mediaplayer.hasMedia()) mediaplayer.stop()
-        playbackState = PlaybackStateCompat.STATE_STOPPED
-        currentTime = 0
+        setPlaybackStopped()
     }
 
     fun releaseMedia() = mediaplayer.media?.let {
@@ -68,6 +68,7 @@ class PlayerController : IVLCVout.Callback, MediaPlayer.EventListener {
         seekable = true
         pausable = true
         currentTime = 0L
+        length = media.duration
         mediaplayer.setEventListener(null)
         mediaplayer.media = media.apply { if (hasRenderer) parse() }
         mediaplayer.setEventListener(this at PlayerController)
@@ -85,7 +86,7 @@ class PlayerController : IVLCVout.Callback, MediaPlayer.EventListener {
         release(mp)
     }
 
-    fun seek(position: Long, length: Double = getLength().toDouble()) {
+    fun seek(position: Long, length: Double = this.length.toDouble()) {
         if (length > 0.0) setPosition((position / length).toFloat())
         else setTime(position)
     }
@@ -172,8 +173,7 @@ class PlayerController : IVLCVout.Callback, MediaPlayer.EventListener {
                 }
             } else player.release()
         }
-        playbackState = PlaybackStateCompat.STATE_STOPPED
-        currentTime = 0L
+        setPlaybackStopped()
     }
 
     fun setSlaves(media: MediaWrapper) = launch {
@@ -215,8 +215,6 @@ class PlayerController : IVLCVout.Callback, MediaPlayer.EventListener {
         return id != Media.Meta.NowPlaying || mw?.nowPlaying !== null
     }
 
-    fun getLength() = if (mediaplayer.hasMedia()) mediaplayer.length else 0L
-
     fun setPreviousStats() {
         val media = mediaplayer.media ?: return
         previousMediaStats = media.stats
@@ -264,14 +262,21 @@ class PlayerController : IVLCVout.Callback, MediaPlayer.EventListener {
         when(event.type) {
             MediaPlayer.Event.Playing -> playbackState = PlaybackStateCompat.STATE_PLAYING
             MediaPlayer.Event.Paused -> playbackState = PlaybackStateCompat.STATE_PAUSED
-            MediaPlayer.Event.EncounteredError -> playbackState = PlaybackStateCompat.STATE_STOPPED
+            MediaPlayer.Event.EncounteredError -> setPlaybackStopped()
             MediaPlayer.Event.PausableChanged -> pausable = event.pausable
             MediaPlayer.Event.SeekableChanged -> seekable = event.seekable
             MediaPlayer.Event.TimeChanged -> currentTime = event.timeChanged
+            MediaPlayer.Event.LengthChanged -> length = event.lengthChanged
         }
         mediaplayerEventListener?.onEvent(event)
     }
 
+    private fun setPlaybackStopped() {
+        playbackState = PlaybackStateCompat.STATE_STOPPED
+        currentTime = 0L
+        length = 0L
+    }
+
 //    private fun onPlayerError() {
 //        launch(UI) {
 //            restart()
diff --git a/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt b/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
index eff3db8f2..01453401c 100644
--- a/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
+++ b/vlc-android/src/org/videolan/vlc/media/PlaylistManager.kt
@@ -362,7 +362,7 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
         if (media.type == MediaWrapper.TYPE_VIDEO || canSwitchToVideo || media.isPodcast) {
             //Save progress
             val time = player.getTime()
-            val length = player.getLength()
+            val length = player.length
             var progress = time / length.toFloat()
             if (progress > 0.95f || length - time < 10000) {
                 //increase seen counter if more than 95% of the media have been seen
@@ -531,10 +531,10 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
     private fun seekToResume(media: MediaWrapper) {
         var mw = media
         if (savedTime > 0L) {
-            if (savedTime < 0.95 * player.getLength()) player.seek(savedTime)
+            if (savedTime < 0.95 * player.length) player.seek(savedTime)
             savedTime = 0L
         } else {
-            val length = player.getLength()
+            val length = player.length
             if (mw.length <= 0L && length > 0L) {
                 mw = medialibrary.findMedia(mw)
                 if (mw.id != 0L) {



More information about the Android mailing list