[Android] Create a PlaybackBottomSheetDialogFragment that listens to playback events

Nicolas Pomepuy git at videolan.org
Mon Feb 17 10:24:56 UTC 2025


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Mon Feb 17 08:51:57 2025 +0100| [16ec4da188a686eba04e09fee42963fc9f036831] | committer: Duncan McNamara

Create a PlaybackBottomSheetDialogFragment that listens to playback events

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

 .../dialogs/PlaybackBottomSheetDialogFragment.kt   | 92 ++++++++++++++++++++++
 .../vlc/gui/dialogs/PlaybackSpeedDialog.kt         | 44 ++---------
 2 files changed, 99 insertions(+), 37 deletions(-)

diff --git a/application/vlc-android/src/org/videolan/vlc/gui/dialogs/PlaybackBottomSheetDialogFragment.kt b/application/vlc-android/src/org/videolan/vlc/gui/dialogs/PlaybackBottomSheetDialogFragment.kt
new file mode 100644
index 0000000000..6bd3ff0617
--- /dev/null
+++ b/application/vlc-android/src/org/videolan/vlc/gui/dialogs/PlaybackBottomSheetDialogFragment.kt
@@ -0,0 +1,92 @@
+/*
+ * ************************************************************************
+ *  PlaybackBottomSheetDialogFragment.kt
+ * *************************************************************************
+ * Copyright © 2025 VLC authors and VideoLAN
+ * Author: Nicolas POMEPUY
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * **************************************************************************
+ *
+ *
+ */
+
+package org.videolan.vlc.gui.dialogs
+
+import android.os.Bundle
+import android.view.View
+import androidx.lifecycle.lifecycleScope
+import kotlinx.coroutines.flow.onEach
+import org.videolan.libvlc.MediaPlayer
+import org.videolan.libvlc.interfaces.IMedia
+import org.videolan.vlc.PlaybackService
+import org.videolan.vlc.util.launchWhenStarted
+
+abstract class PlaybackBottomSheetDialogFragment:VLCBottomSheetDialogFragment(), PlaybackService.Callback {
+
+    var playbackService: PlaybackService? = null
+
+    /**
+     * Triggered when the service is available
+     *
+     */
+    abstract fun onServiceAvailable()
+
+    /**
+     * Triggered when the current media changes
+     *
+     */
+    abstract fun onMediaChanged()
+    open val dismissOnServiceEnded = true
+    open val dismissOnPlaybackEnded = true
+
+
+    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
+        super.onViewCreated(view, savedInstanceState)
+        PlaybackService.serviceFlow.onEach { onServiceChanged(it) }.launchWhenStarted(lifecycleScope)
+    }
+
+    private fun onServiceChanged(service: PlaybackService?) {
+        if (service != null) {
+            playbackService = service
+            playbackService!!.addCallback(this)
+            onServiceAvailable()
+        } else {
+            this.playbackService?.apply {
+                removeCallback(this at PlaybackBottomSheetDialogFragment)
+            }
+            playbackService = null
+            if (dismissOnServiceEnded) dismiss()
+        }
+    }
+
+    override fun onDestroy() {
+        this.playbackService?.apply {
+            removeCallback(this at PlaybackBottomSheetDialogFragment)
+        }
+        super.onDestroy()
+    }
+
+    override fun update() {
+        if (playbackService?.playlistManager?.hasCurrentMedia() == true)
+            onMediaChanged()
+        else if (dismissOnPlaybackEnded)
+            dismiss()
+    }
+
+    override fun onMediaEvent(event: IMedia.Event) { }
+
+    override fun onMediaPlayerEvent(event: MediaPlayer.Event) { }
+
+}
\ No newline at end of file
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/dialogs/PlaybackSpeedDialog.kt b/application/vlc-android/src/org/videolan/vlc/gui/dialogs/PlaybackSpeedDialog.kt
index f89313f2e1..680fe90b75 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/dialogs/PlaybackSpeedDialog.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/dialogs/PlaybackSpeedDialog.kt
@@ -53,7 +53,7 @@ import kotlin.math.ln
 import kotlin.math.pow
 
 
-class PlaybackSpeedDialog : VLCBottomSheetDialogFragment(), PlaybackService.Callback {
+class PlaybackSpeedDialog : PlaybackBottomSheetDialogFragment(), PlaybackService.Callback {
 
     private lateinit var settings: SharedPreferences
     private val forVideo: Boolean
@@ -62,7 +62,6 @@ class PlaybackSpeedDialog : VLCBottomSheetDialogFragment(), PlaybackService.Call
         }
     private lateinit var binding: DialogPlaybackSpeedBinding
 
-    private var playbackService: PlaybackService? = null
     private var textColor: Int = 0
 
     private val orangeColor:Int
@@ -198,9 +197,12 @@ class PlaybackSpeedDialog : VLCBottomSheetDialogFragment(), PlaybackService.Call
         }
     }
 
-    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
-        super.onViewCreated(view, savedInstanceState)
-        PlaybackService.serviceFlow.onEach { onServiceChanged(it) }.launchWhenStarted(lifecycleScope)
+    override fun onServiceAvailable() {
+        setRateProgress()
+    }
+
+    override fun onMediaChanged() {
+        setRateProgress()
     }
 
     private fun setRateProgress() {
@@ -255,27 +257,6 @@ class PlaybackSpeedDialog : VLCBottomSheetDialogFragment(), PlaybackService.Call
 
     }
 
-    override fun onDestroy() {
-        this.playbackService?.apply {
-            removeCallback(this at PlaybackSpeedDialog)
-        }
-        super.onDestroy()
-    }
-
-    private fun onServiceChanged(service: PlaybackService?) {
-        if (service != null) {
-            playbackService = service
-            playbackService!!.addCallback(this)
-            setRateProgress()
-        } else {
-            this.playbackService?.apply {
-                removeCallback(this at PlaybackSpeedDialog)
-            }
-            playbackService = null
-            dismiss()
-        }
-    }
-
     override fun getDefaultState(): Int {
         return com.google.android.material.bottomsheet.BottomSheetBehavior.STATE_EXPANDED
     }
@@ -286,17 +267,6 @@ class PlaybackSpeedDialog : VLCBottomSheetDialogFragment(), PlaybackService.Call
         return true
     }
 
-    override fun update() {
-        if (playbackService?.playlistManager?.hasCurrentMedia() == true)
-            setRateProgress()
-        else
-            dismiss()
-    }
-
-    override fun onMediaEvent(event: IMedia.Event) { }
-
-    override fun onMediaPlayerEvent(event: MediaPlayer.Event) { }
-
     companion object {
 
         const val TAG = "VLC/PlaybackSpeedDialog"



More information about the Android mailing list