[Android] Prevent haptic feedback when volume is set from hardware keys

Nicolas Pomepuy git at videolan.org
Tue Jul 7 13:16:03 CEST 2020


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Fri Jul  3 08:46:20 2020 +0200| [890bae8d476f0b620d55e44a128cd2372c1356d8] | committer: Nicolas Pomepuy

Prevent haptic feedback when volume is set from hardware keys

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

 .../src/org/videolan/vlc/gui/video/VideoPlayerActivity.kt    | 12 ++++++------
 .../org/videolan/vlc/gui/video/VideoPlayerOverlayDelegate.kt |  4 ++--
 .../src/org/videolan/vlc/gui/video/VideoTouchDelegate.kt     |  8 ++++----
 3 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/application/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.kt b/application/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.kt
index 743d15e37..0d3f0fa53 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.kt
@@ -1127,8 +1127,8 @@ open class VideoPlayerActivity : AppCompatActivity(), PlaybackService.Callback,
             var volume = if (audiomanager.getStreamVolume(AudioManager.STREAM_MUSIC) < audioMax)
                 audiomanager.getStreamVolume(AudioManager.STREAM_MUSIC) + 1
             else
-                Math.round(service.volume.toFloat() * audioMax / 100 + 1)
-            volume = Math.min(Math.max(volume, 0), audioMax * if (isAudioBoostEnabled) 2 else 1)
+                (service.volume.toFloat() * audioMax / 100 + 1).roundToInt()
+            volume = volume.coerceAtLeast(0).coerceAtMost(audioMax * if (isAudioBoostEnabled) 2 else 1)
             setAudioVolume(volume)
         }
     }
@@ -1136,10 +1136,10 @@ open class VideoPlayerActivity : AppCompatActivity(), PlaybackService.Callback,
     private fun volumeDown() {
         service?.let { service ->
             var vol = if (service.volume > 100)
-                Math.round(service.volume.toFloat() * audioMax / 100 - 1)
+                (service.volume.toFloat() * audioMax / 100 - 1).roundToInt()
             else
                 audiomanager.getStreamVolume(AudioManager.STREAM_MUSIC) - 1
-            vol = Math.min(Math.max(vol, 0), audioMax * if (isAudioBoostEnabled) 2 else 1)
+            vol = vol.coerceAtLeast(0).coerceAtMost(audioMax * if (isAudioBoostEnabled) 2 else 1)
             originalVol = vol.toFloat()
             setAudioVolume(vol)
         }
@@ -1375,7 +1375,7 @@ open class VideoPlayerActivity : AppCompatActivity(), PlaybackService.Callback,
         }
     }
 
-    internal fun setAudioVolume(volume: Int) {
+    internal fun setAudioVolume(volume: Int, fromTouch:Boolean = false) {
         var vol = volume
         if (AndroidUtil.isNougatOrLater && (vol <= 0) xor isMute) {
             mute(!isMute)
@@ -1402,7 +1402,7 @@ open class VideoPlayerActivity : AppCompatActivity(), PlaybackService.Callback,
                 vol = (vol * 100 / audioMax.toFloat()).roundToInt()
                 service.setVolume(vol.toFloat().roundToInt())
             }
-            overlayDelegate.showVolumeBar(vol)
+            overlayDelegate.showVolumeBar(vol, fromTouch)
         }
     }
 
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerOverlayDelegate.kt b/application/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerOverlayDelegate.kt
index f5ca8ad5d..1643a7b95 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerOverlayDelegate.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerOverlayDelegate.kt
@@ -195,12 +195,12 @@ class VideoPlayerOverlayDelegate (private val player: VideoPlayerActivity) {
      * Show the volume value with  bar
      * @param volume the volume value
      */
-    fun showVolumeBar(volume: Int) {
+    fun showVolumeBar(volume: Int, fromTouch: Boolean) {
         player.findViewById<ViewStubCompat>(R.id.player_volume_stub)?.setVisible()
         playerOverlayVolume = player.findViewById(R.id.player_overlay_volume)
         volumeValueText = player.findViewById(R.id.volume_value_text)
         playerVolumeProgress = player.findViewById(R.id.playerVolumeProgress)
-        if (playerOverlayVolume.visibility != View.VISIBLE)  hapticFeedback()
+        if (playerOverlayVolume.visibility != View.VISIBLE && fromTouch)  hapticFeedback()
         volumeValueText.text = "$volume%"
         playerVolumeProgress.isDouble = player.isAudioBoostEnabled
         playerVolumeProgress.setValue(volume)
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/video/VideoTouchDelegate.kt b/application/vlc-android/src/org/videolan/vlc/gui/video/VideoTouchDelegate.kt
index 6cdd02d98..16d4a8c99 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/video/VideoTouchDelegate.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/video/VideoTouchDelegate.kt
@@ -287,7 +287,7 @@ class VideoTouchDelegate(private val player: VideoPlayerActivity,
                 player.volume = player.audiomanager.getStreamVolume(AudioManager.STREAM_MUSIC).toFloat()
                 val delta = -(rz / 7 * player.audioMax).toInt()
                 val vol = (player.volume.toInt() + delta).coerceIn(0, player.audioMax)
-                player.setAudioVolume(vol)
+                player.setAudioVolume(vol, true)
             }
             lastMove = System.currentTimeMillis()
         }
@@ -361,14 +361,14 @@ class VideoTouchDelegate(private val player: VideoPlayerActivity,
                 if (player.isAudioBoostEnabled) {
                     if (player.originalVol < audioMax) {
                         player.displayWarningToast()
-                        player.setAudioVolume(audioMax)
+                        player.setAudioVolume(audioMax, true)
                     } else {
-                        player.setAudioVolume(vol)
+                        player.setAudioVolume(vol, true)
                     }
                     touchAction = TOUCH_VOLUME
                 }
             } else {
-                player.setAudioVolume(vol)
+                player.setAudioVolume(vol, true)
                 touchAction = TOUCH_VOLUME
             }
         }



More information about the Android mailing list