[Android] Display equalizer preamp value in the progressbar thumb

Nicolas Pomepuy git at videolan.org
Tue Dec 21 09:55:47 UTC 2021


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Mon Dec 20 13:27:37 2021 +0100| [32046910493c2448f21076c180f96268853a6ba8] | committer: Nicolas Pomepuy

Display equalizer preamp value in the progressbar thumb

Fixes #2133

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

 application/vlc-android/res/layout/equalizer.xml   |  7 +-
 .../videolan/vlc/gui/audio/EqualizerFragment.kt    | 76 ++++++++++------------
 2 files changed, 41 insertions(+), 42 deletions(-)

diff --git a/application/vlc-android/res/layout/equalizer.xml b/application/vlc-android/res/layout/equalizer.xml
index 18bda540f..30ba2e0e4 100644
--- a/application/vlc-android/res/layout/equalizer.xml
+++ b/application/vlc-android/res/layout/equalizer.xml
@@ -87,7 +87,7 @@
                     app:layout_constraintStart_toStartOf="parent"
                     app:layout_constraintTop_toTopOf="@+id/equalizer_preamp" />
 
-            <SeekBar
+            <com.google.android.material.slider.Slider
                     android:id="@+id/equalizer_preamp"
                     android:layout_width="0dp"
                     android:layout_height="wrap_content"
@@ -96,6 +96,11 @@
                     android:layout_marginEnd="8dp"
                     android:maxHeight="4dip"
                     android:minHeight="4dip"
+                    android:valueFrom="-20"
+                    android:valueTo="20"
+                    android:stepSize="1"
+                    app:tickVisible="false"
+                    android:contentDescription="@string/preamp"
                     android:progressDrawable="@drawable/po_seekbar"
                     android:splitTrack="false"
                     android:thumb="@drawable/ic_seekbar_thumb"
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/audio/EqualizerFragment.kt b/application/vlc-android/src/org/videolan/vlc/gui/audio/EqualizerFragment.kt
index 7651a94df..46bf5b4b4 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/audio/EqualizerFragment.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/audio/EqualizerFragment.kt
@@ -37,6 +37,7 @@ import androidx.databinding.DataBindingUtil
 import androidx.databinding.ObservableBoolean
 import androidx.lifecycle.lifecycleScope
 import com.google.android.material.bottomsheet.BottomSheetBehavior.STATE_EXPANDED
+import com.google.android.material.slider.Slider
 import kotlinx.coroutines.*
 import org.videolan.libvlc.MediaPlayer
 import org.videolan.resources.AppContextProvider
@@ -53,10 +54,11 @@ import org.videolan.vlc.gui.helpers.UiTools
 import org.videolan.vlc.gui.view.EqualizerBar
 import org.videolan.vlc.interfaces.OnEqualizerBarChangeListener
 import java.lang.Runnable
+import kotlin.math.roundToInt
 
 @ObsoleteCoroutinesApi
 @ExperimentalCoroutinesApi
-class EqualizerFragment : VLCBottomSheetDialogFragment() {
+class EqualizerFragment : VLCBottomSheetDialogFragment(), Slider.OnChangeListener {
     override fun getDefaultState() = STATE_EXPANDED
 
     override fun needToManageOrientation() = false
@@ -73,7 +75,7 @@ class EqualizerFragment : VLCBottomSheetDialogFragment() {
     private var updateAlreadyHandled = false
     private lateinit var binding: EqualizerBinding
     private val state = EqualizerState()
-    private val newPresetName = AppContextProvider.appResources.getString(org.videolan.vlc.R.string.equalizer_new_preset_name)
+    private val newPresetName = AppContextProvider.appResources.getString(R.string.equalizer_new_preset_name)
     private var bandCount = -1
 
     private val eqBandsViews = ArrayList<EqualizerBar>()
@@ -92,36 +94,9 @@ class EqualizerFragment : VLCBottomSheetDialogFragment() {
         override fun onNothingSelected(parent: AdapterView<*>) {}
     }
 
-    private val mPreampListener = object : OnSeekBarChangeListener {
-        override fun onStopTrackingTouch(seekBar: SeekBar) {}
-
-        override fun onStartTrackingTouch(seekBar: SeekBar) {}
-
-        override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
-            if (!fromUser) return
-            equalizer.preAmp = (progress - 20).toFloat()
-            if (!binding.equalizerButton.isChecked) binding.equalizerButton.isChecked = true
-
-            val pos = binding.equalizerPresets.selectedItemPosition
-            if (getEqualizerType(pos) == TYPE_PRESET) {
-                revertPos = pos
-                savePos = presetCount + customCount
-                state.update(presetCount + customCount, false)
-                updateAlreadyHandled = true
-                binding.equalizerPresets.setSelection(presetCount + customCount)
-            } else if (getEqualizerType(pos) == TYPE_CUSTOM) {
-                revertPos = pos
-                savePos = pos
-                state.update(pos, false)
-            }
-            if (binding.equalizerButton.isChecked) PlaybackService.equalizer.value = equalizer
-        }
-    }
-
-
     override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
         super.onCreateView(inflater, container, savedInstanceState)
-        binding = DataBindingUtil.inflate(inflater, org.videolan.vlc.R.layout.equalizer, container, false)
+        binding = DataBindingUtil.inflate(inflater, R.layout.equalizer, container, false)
         binding.state = state
         return binding.root
     }
@@ -172,9 +147,8 @@ class EqualizerFragment : VLCBottomSheetDialogFragment() {
         binding.equalizerPresets.adapter = adapter
 
         // preamp
-        binding.equalizerPreamp.max = 40
-        binding.equalizerPreamp.progress = equalizer.preAmp.toInt() + 20
-        binding.equalizerPreamp.setOnSeekBarChangeListener(mPreampListener)
+        binding.equalizerPreamp.value = equalizer.preAmp.roundToInt().toFloat()
+        binding.equalizerPreamp.addOnChangeListener(this at EqualizerFragment)
 
         eqBandsViews.clear()
         binding.equalizerBands.removeAllViews()
@@ -193,8 +167,8 @@ class EqualizerFragment : VLCBottomSheetDialogFragment() {
             bar.setListener(BandListener(i))
         }
 
-        eqBandsViews[0].nextFocusLeftId = org.videolan.vlc.R.id.equalizer_preamp
-        eqBandsViews[eqBandsViews.size - 1].nextFocusRightId = org.videolan.vlc.R.id.snapBands
+        eqBandsViews[0].nextFocusLeftId = R.id.equalizer_preamp
+        eqBandsViews[eqBandsViews.size - 1].nextFocusRightId = R.id.snapBands
 
         // Set the default selection asynchronously to prevent a layout initialization bug.
         binding.equalizerPresets.post {
@@ -222,7 +196,7 @@ class EqualizerFragment : VLCBottomSheetDialogFragment() {
         super.onPause()
         binding.equalizerButton.setOnCheckedChangeListener(null)
         binding.equalizerPresets.onItemSelectedListener = null
-        binding.equalizerPreamp.setOnSeekBarChangeListener(null)
+        binding.equalizerPreamp.removeOnChangeListener(this)
         if (binding.equalizerButton.isChecked) {
             val pos = binding.equalizerPresets.selectedItemPosition
             if (::equalizer.isInitialized) VLCOptions.saveEqualizerInSettings(requireActivity(), equalizer, allSets[pos], true, state.saved)
@@ -331,7 +305,7 @@ class EqualizerFragment : VLCBottomSheetDialogFragment() {
 
 
         val container = FrameLayout(requireContext())
-        val klNormal = resources.getDimension(org.videolan.vlc.R.dimen.kl_normal).toInt()
+        val klNormal = resources.getDimension(R.dimen.kl_normal).toInt()
         container.setPadding(klNormal, 0, klNormal, 0)
 
         container.addView(input)
@@ -424,7 +398,7 @@ class EqualizerFragment : VLCBottomSheetDialogFragment() {
             customCount--
             state.update(0, true)
             binding.equalizerPresets.setSelection(0)
-            val message = getString(org.videolan.vlc.R.string.custom_set_deleted_message, oldName)
+            val message = getString(R.string.custom_set_deleted_message, oldName)
             UiTools.snackerWithCancel(requireActivity(), message, null, cancelAction)
         }
     }
@@ -453,9 +427,9 @@ class EqualizerFragment : VLCBottomSheetDialogFragment() {
             binding.equalizerPresets.setSelection(revertPos)
 
         val message = if (getEqualizerType(pos) == TYPE_CUSTOM)
-            getString(org.videolan.vlc.R.string.custom_set_restored)
+            getString(R.string.custom_set_restored)
         else
-            getString(org.videolan.vlc.R.string.unsaved_set_deleted_message)
+            getString(R.string.unsaved_set_deleted_message)
         UiTools.snackerWithCancel(requireActivity(), message, null, cancelAction)
     }
 
@@ -484,7 +458,7 @@ class EqualizerFragment : VLCBottomSheetDialogFragment() {
         }
         if (!isStarted()) return at launch
 
-        binding.equalizerPreamp.progress = equalizer.preAmp.toInt() + 20
+        binding.equalizerPreamp.value = equalizer.preAmp.roundToInt().toFloat()
         for (i in 0 until bandCount) {
             val bar = binding.equalizerBands.getChildAt(i) as EqualizerBar
             bar.setValue(equalizer.getAmp(i))
@@ -532,4 +506,24 @@ class EqualizerFragment : VLCBottomSheetDialogFragment() {
                 return presets.toTypedArray()
             }
     }
+
+    override fun onValueChange(slider: Slider, value: Float, fromUser: Boolean) {
+        if (!fromUser) return
+        equalizer.preAmp = binding.equalizerPreamp.value
+        if (!binding.equalizerButton.isChecked) binding.equalizerButton.isChecked = true
+
+        val pos = binding.equalizerPresets.selectedItemPosition
+        if (getEqualizerType(pos) == TYPE_PRESET) {
+            revertPos = pos
+            savePos = presetCount + customCount
+            state.update(presetCount + customCount, false)
+            updateAlreadyHandled = true
+            binding.equalizerPresets.setSelection(presetCount + customCount)
+        } else if (getEqualizerType(pos) == TYPE_CUSTOM) {
+            revertPos = pos
+            savePos = pos
+            state.update(pos, false)
+        }
+        if (binding.equalizerButton.isChecked) PlaybackService.equalizer.value = equalizer
+    }
 }
\ No newline at end of file



More information about the Android mailing list