[Android] Cache the current media chapters to avoid native calls

Nicolas Pomepuy git at videolan.org
Thu Sep 5 08:48:58 UTC 2024


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Thu Aug  8 07:59:18 2024 +0200| [28f60942f866ec68c1226c067f11d08fcd1fe143] | committer: Nicolas Pomepuy

Cache the current media chapters to avoid native calls

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

 .../src/org/videolan/vlc/gui/audio/AudioPlayer.kt    | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)

diff --git a/application/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.kt b/application/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.kt
index 52fe3490e9..d65bf2e4de 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.kt
@@ -57,6 +57,7 @@ import com.google.android.material.bottomsheet.BottomSheetBehavior
 import kotlinx.coroutines.*
 import kotlinx.coroutines.flow.conflate
 import kotlinx.coroutines.flow.onEach
+import org.videolan.libvlc.MediaPlayer
 import org.videolan.medialibrary.Tools
 import org.videolan.medialibrary.interfaces.media.MediaWrapper
 import org.videolan.resources.*
@@ -123,6 +124,7 @@ class AudioPlayer : Fragment(), PlaylistAdapter.IPlayer, TextWatcher, IAudioPlay
     private var audioPlayProgressMode:Boolean = false
     private var lastEndsAt = -1L
     private var isDragging = false
+    private var currentChapters: Pair<MediaWrapper,  List<MediaPlayer.Chapter>?>? = null
 
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
@@ -275,6 +277,7 @@ class AudioPlayer : Fragment(), PlaylistAdapter.IPlayer, TextWatcher, IAudioPlay
     override fun onDestroy() {
         Settings.removeAudioControlsChangeListener()
         binding.songsList.adapter = null
+        currentChapters = null
         super.onDestroy()
     }
 
@@ -896,6 +899,7 @@ class AudioPlayer : Fragment(), PlaylistAdapter.IPlayer, TextWatcher, IAudioPlay
 
     private val coverMediaSwitcherListener = object : AudioMediaSwitcherListener by AudioMediaSwitcher.EmptySwitcherListener {
 
+
         override fun onMediaSwitching() {
             (activity as? AudioPlayerContainerActivity)?.playerBehavior?.lock(true)
         }
@@ -915,8 +919,20 @@ class AudioPlayer : Fragment(), PlaylistAdapter.IPlayer, TextWatcher, IAudioPlay
         }
 
         override fun onChapterSwitching(next: Boolean) {
-            playlistModel.service?.let {service ->
-                service.chapterIdx = service.chapterIdx.plus(if (next) 1 else -1).coerceIn(0, service.mediaplayer.getChapters(-1).size - 1)
+            playlistModel.service?.let { service ->
+                service.currentMediaWrapper?.let { media ->
+                    if (currentChapters?.first?.uri != media.uri) {
+                        playlistModel.service?.getChapters(-1)?.let {
+                            currentChapters = Pair(media, it.toList())
+                        }
+                    }
+                }
+            }
+
+            currentChapters?.second?.let { chapters ->
+                playlistModel.service!!.chapterIdx =
+                    playlistModel.service!!.chapterIdx.plus(if (next) 1 else -1)
+                        .coerceIn(0, chapters.size - 1)
             }
         }
     }



More information about the Android mailing list