[Android] Refactor the audio jump method in the view model
Nicolas Pomepuy
git at videolan.org
Mon Oct 6 11:44:41 UTC 2025
vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Mon Oct 6 12:04:01 2025 +0200| [99ad4a94ca6de72ecce94231fa18023d73f7d358] | committer: Nicolas Pomepuy
Refactor the audio jump method in the view model
The jump code was already duplicated in two places
and will be needed elsewhere in the future
> https://code.videolan.org/videolan/vlc-android/commit/99ad4a94ca6de72ecce94231fa18023d73f7d358
---
.../ui/audioplayer/AudioPlayerActivity.kt | 23 +---------------
.../src/org/videolan/vlc/gui/audio/AudioPlayer.kt | 32 ++++------------------
.../org/videolan/vlc/viewmodels/PlaylistModel.kt | 28 +++++++++++++++++++
3 files changed, 34 insertions(+), 49 deletions(-)
diff --git a/application/television/src/main/java/org/videolan/television/ui/audioplayer/AudioPlayerActivity.kt b/application/television/src/main/java/org/videolan/television/ui/audioplayer/AudioPlayerActivity.kt
index f884a126f5..b6d43502ee 100644
--- a/application/television/src/main/java/org/videolan/television/ui/audioplayer/AudioPlayerActivity.kt
+++ b/application/television/src/main/java/org/videolan/television/ui/audioplayer/AudioPlayerActivity.kt
@@ -392,7 +392,7 @@ class AudioPlayerActivity : BaseTvActivity(),KeycodeListener, PlaybackService.Ca
binding.playbackSpeedQuickAction.isFocusable = !bookmarkListDelegate.visible
}
bookmarkListDelegate.seekListener = { forward, long ->
- jump(forward, long)
+ model.jump(forward, long, this)
}
bookmarkListDelegate.markerContainer = binding.bookmarkMarkerContainer
}
@@ -400,27 +400,6 @@ class AudioPlayerActivity : BaseTvActivity(),KeycodeListener, PlaybackService.Ca
}
}
- /**
- * Jump backward or forward, with a long or small delay
- * depending on the audio control setting chosen by the user
- *
- * @param forward is the jump forward?
- * @param long has it been triggered by a long tap?
- */
- private fun jump(forward:Boolean, long:Boolean) {
- model.service?.let { service ->
- val jumpDelay = if (long) Settings.audioLongJumpDelay else Settings.audioJumpDelay
- val delay = if (forward) jumpDelay * 1000 else -(jumpDelay * 1000)
- var position = service.getTime() + delay
- if (position < 0) position = 0
- if (position > service.length) position = service.length
- service.seek(position, service.length.toDouble(), true, fast = false)
- service.playlistManager.player.updateProgress(position)
- if (service.playlistManager.player.lastPosition == 0.0f && (forward || service.getTime() > 0))
- UiTools.snacker(this, getString(org.videolan.vlc.R.string.unseekable_stream))
- }
- }
-
private fun setShuffleMode(shuffle: Boolean) {
shuffling = shuffle
val medias = model.medias?.toMutableList() ?: return
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 c19d2f64c9..6a0a05679a 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
@@ -708,20 +708,20 @@ class AudioPlayer : Fragment(), PlaylistAdapter.IPlayer, TextWatcher, IAudioPlay
}
fun onJumpBack(@Suppress("UNUSED_PARAMETER") view: View) {
- jump(forward = false, long = false)
+ playlistModel.jump(forward = false, long = false, requireActivity())
}
fun onJumpBackLong(@Suppress("UNUSED_PARAMETER") view: View):Boolean {
- jump(forward = false, long = true)
+ playlistModel.jump(forward = false, long = true, requireActivity())
return true
}
fun onJumpForward(@Suppress("UNUSED_PARAMETER") view: View) {
- jump(forward = true, long = false)
+ playlistModel.jump(forward = true, long = false, requireActivity())
}
fun onJumpForwardLong(@Suppress("UNUSED_PARAMETER") view: View):Boolean {
- jump(forward = true, long = true)
+ playlistModel.jump(forward = true, long = true, requireActivity())
return true
}
@@ -739,28 +739,6 @@ class AudioPlayer : Fragment(), PlaylistAdapter.IPlayer, TextWatcher, IAudioPlay
}
}
- /**
- * Jump backward or forward, with a long or small delay
- * depending on the audio control setting chosen by the user
- *
- * @param forward is the jump forward?
- * @param long has it been triggered by a long tap?
- */
- private fun jump(forward:Boolean, long:Boolean) {
- playlistModel.service ?.let { service ->
- val jumpDelay = if (long) Settings.audioLongJumpDelay else Settings.audioJumpDelay
- var delay = if (forward) jumpDelay * 1000 else -(jumpDelay * 1000)
- if (LocaleUtil.isRtl()) delay = -delay
- var position = service.getTime() + delay
- if (position < 0) position = 0
- if (position > service.length) position = service.length
- service.seek(position, service.length.toDouble(), true, fast = false)
- service.playlistManager.player.updateProgress(position)
- if (service.playlistManager.player.lastPosition == 0.0f && (forward || service.getTime() > 0))
- UiTools.snacker(requireActivity(), getString(R.string.unseekable_stream))
- }
- }
-
fun onPlayPauseClick(@Suppress("UNUSED_PARAMETER") view: View?) {
if (playlistModel.service?.isPausable == false) {
UiTools.snackerConfirm(requireActivity(), getString(R.string.stop_unpaubale), true) {
@@ -843,7 +821,7 @@ class AudioPlayer : Fragment(), PlaylistAdapter.IPlayer, TextWatcher, IAudioPlay
}
}
bookmarkListDelegate.seekListener = { forward, long ->
- jump(forward , long)
+ playlistModel.jump(forward , long, requireActivity())
}
bookmarkListDelegate.markerContainer = binding.bookmarkMarkerContainer
}
diff --git a/application/vlc-android/src/org/videolan/vlc/viewmodels/PlaylistModel.kt b/application/vlc-android/src/org/videolan/vlc/viewmodels/PlaylistModel.kt
index 87d064b2ef..2ea1cc74f0 100644
--- a/application/vlc-android/src/org/videolan/vlc/viewmodels/PlaylistModel.kt
+++ b/application/vlc-android/src/org/videolan/vlc/viewmodels/PlaylistModel.kt
@@ -20,6 +20,7 @@
package org.videolan.vlc.viewmodels
+import android.app.Activity
import android.support.v4.media.session.PlaybackStateCompat
import androidx.annotation.MainThread
import androidx.fragment.app.Fragment
@@ -38,10 +39,14 @@ import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.withContext
import org.videolan.medialibrary.Tools
import org.videolan.medialibrary.interfaces.media.MediaWrapper
+import org.videolan.tools.Settings
import org.videolan.tools.livedata.LiveDataset
import org.videolan.vlc.PlaybackService
+import org.videolan.vlc.R
+import org.videolan.vlc.gui.helpers.UiTools
import org.videolan.vlc.media.PlaylistManager
import org.videolan.vlc.util.EmptyPBSCallback
+import org.videolan.vlc.util.LocaleUtil
import org.videolan.vlc.util.PlaylistFilterDelegate
class PlaylistModel : ViewModel(), PlaybackService.Callback by EmptyPBSCallback {
@@ -84,6 +89,29 @@ class PlaylistModel : ViewModel(), PlaybackService.Callback by EmptyPBSCallback
update()
}
+ /**
+ * Jump backward or forward, with a long or small delay
+ * depending on the audio control setting chosen by the user
+ *
+ * @param forward is the jump forward?
+ * @param long has it been triggered by a long tap?
+ * @param activity the activity used to display the snackbar if needed
+ */
+ fun jump(forward:Boolean, long:Boolean, activity: Activity) {
+ service ?.let { service ->
+ val jumpDelay = if (long) Settings.audioLongJumpDelay else Settings.audioJumpDelay
+ var delay = if (forward) jumpDelay * 1000 else -(jumpDelay * 1000)
+ if (LocaleUtil.isRtl()) delay = -delay
+ var position = service.getTime() + delay
+ if (position < 0) position = 0
+ if (position > service.length) position = service.length
+ service.seek(position, service.length.toDouble(), true, fast = false)
+ service.playlistManager.player.updateProgress(position)
+ if (service.playlistManager.player.lastPosition == 0.0f && (forward || service.getTime() > 0))
+ UiTools.snacker(activity, activity.getString(R.string.unseekable_stream))
+ }
+ }
+
override fun update() {
service?.run {
if (filtering) {
More information about the Android
mailing list