[Android] Fix key listener crash when the audio player is not ready

Nicolas Pomepuy git at videolan.org
Wed Mar 30 09:28:38 UTC 2022


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Wed Mar 30 11:06:17 2022 +0200| [770db9d27f0973c202afb1fe51cda6e6cdc80fc9] | committer: Nicolas Pomepuy

Fix key listener crash when the audio player is not ready

> https://code.videolan.org/videolan/vlc-android/commit/770db9d27f0973c202afb1fe51cda6e6cdc80fc9
---

 .../ui/audioplayer/AudioPlayerActivity.kt          |   2 +
 .../vlc/gui/AudioPlayerContainerActivity.kt        |   2 +
 .../vlc/gui/helpers/PlayerKeyListenerDelegate.kt   | 131 +++++++++++----------
 .../videolan/vlc/gui/video/VideoPlayerActivity.kt  |   2 +
 4 files changed, 77 insertions(+), 60 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 ba7019627..ca233c692 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
@@ -231,6 +231,8 @@ class AudioPlayerActivity : BaseTvActivity(),KeycodeListener  {
         model.setTime(time.toLong())
     }
 
+    override fun isReady() = true
+
     override fun showAdvancedOptions() {
         showAdvancedOptions(null)
     }
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/AudioPlayerContainerActivity.kt b/application/vlc-android/src/org/videolan/vlc/gui/AudioPlayerContainerActivity.kt
index 23d71e6b3..927643325 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/AudioPlayerContainerActivity.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/AudioPlayerContainerActivity.kt
@@ -266,6 +266,8 @@ open class AudioPlayerContainerActivity : BaseActivity(), KeycodeListener {
         return super.onKeyDown(keyCode, event)
     }
 
+    override fun isReady() = ::audioPlayer.isInitialized
+
     override fun showAdvancedOptions() {
         audioPlayer.showAdvancedOptions(null)
     }
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/helpers/PlayerKeyListenerDelegate.kt b/application/vlc-android/src/org/videolan/vlc/gui/helpers/PlayerKeyListenerDelegate.kt
index bbc8f565c..cc27a6cad 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/helpers/PlayerKeyListenerDelegate.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/helpers/PlayerKeyListenerDelegate.kt
@@ -37,70 +37,74 @@ class PlayerKeyListenerDelegate(private val keycodeListener: KeycodeListener) {
      * @param keyCode: the keycode from the event
      * @param event: the key event with modifiers etc
      *
-     * @return true if the vent was consumed, false otherwise
+     * @return true if the event was consumed, false otherwise
      */
-    fun onKeyDown(keyCode: Int, event: KeyEvent) = when (keyCode) {
-        KeyEvent.KEYCODE_O, KeyEvent.KEYCODE_BUTTON_Y, KeyEvent.KEYCODE_MENU -> {
-            keycodeListener.showAdvancedOptions()
-            true
-        }
-        KeyEvent.KEYCODE_MEDIA_PLAY, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, KeyEvent.KEYCODE_MEDIA_PAUSE, KeyEvent.KEYCODE_SPACE -> {
-            keycodeListener.togglePlayPause()
-            true
-        }
-        KeyEvent.KEYCODE_N, KeyEvent.KEYCODE_BUTTON_R2, KeyEvent.KEYCODE_CHANNEL_UP -> {
-            keycodeListener.next()
-            true
-        }
-        KeyEvent.KEYCODE_MEDIA_FAST_FORWARD -> {
-            keycodeListener.seek(10000)
-            true
-        }
-        KeyEvent.KEYCODE_MEDIA_REWIND -> {
-            keycodeListener.seek(-10000)
-            true
-        }
-        KeyEvent.KEYCODE_BUTTON_R1 -> {
-            keycodeListener.seek(60000)
-            true
-        }
-        KeyEvent.KEYCODE_BUTTON_L1 -> {
-            keycodeListener.seek(-60000)
-            true
-        }
-        KeyEvent.KEYCODE_S, KeyEvent.KEYCODE_MEDIA_STOP -> {
-            keycodeListener.stop()
-            true
-        }
-        KeyEvent.KEYCODE_P, KeyEvent.KEYCODE_BUTTON_L2, KeyEvent.KEYCODE_CHANNEL_DOWN -> {
-            keycodeListener.previous()
-            true
-        }
-        KeyEvent.KEYCODE_E -> {
-            if (event.isCtrlPressed) {
-                keycodeListener.showEqualizer()
+    fun onKeyDown(keyCode: Int, event: KeyEvent):Boolean {
+        if (!keycodeListener.isReady()) return false
+        return when (keyCode) {
+
+            KeyEvent.KEYCODE_O, KeyEvent.KEYCODE_BUTTON_Y, KeyEvent.KEYCODE_MENU -> {
+                keycodeListener.showAdvancedOptions()
+                true
             }
-            true
-        }
-        KeyEvent.KEYCODE_PLUS -> {
-            keycodeListener.increaseRate()
-            true
-        }
-        KeyEvent.KEYCODE_EQUALS -> {
-            if (event.isShiftPressed) {
+            KeyEvent.KEYCODE_MEDIA_PLAY, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, KeyEvent.KEYCODE_MEDIA_PAUSE, KeyEvent.KEYCODE_SPACE -> {
+                keycodeListener.togglePlayPause()
+                true
+            }
+            KeyEvent.KEYCODE_N, KeyEvent.KEYCODE_BUTTON_R2, KeyEvent.KEYCODE_CHANNEL_UP -> {
+                keycodeListener.next()
+                true
+            }
+            KeyEvent.KEYCODE_MEDIA_FAST_FORWARD -> {
+                keycodeListener.seek(10000)
+                true
+            }
+            KeyEvent.KEYCODE_MEDIA_REWIND -> {
+                keycodeListener.seek(-10000)
+                true
+            }
+            KeyEvent.KEYCODE_BUTTON_R1 -> {
+                keycodeListener.seek(60000)
+                true
+            }
+            KeyEvent.KEYCODE_BUTTON_L1 -> {
+                keycodeListener.seek(-60000)
+                true
+            }
+            KeyEvent.KEYCODE_S, KeyEvent.KEYCODE_MEDIA_STOP -> {
+                keycodeListener.stop()
+                true
+            }
+            KeyEvent.KEYCODE_P, KeyEvent.KEYCODE_BUTTON_L2, KeyEvent.KEYCODE_CHANNEL_DOWN -> {
+                keycodeListener.previous()
+                true
+            }
+            KeyEvent.KEYCODE_E -> {
+                if (event.isCtrlPressed) {
+                    keycodeListener.showEqualizer()
+                }
+                true
+            }
+            KeyEvent.KEYCODE_PLUS -> {
                 keycodeListener.increaseRate()
-            } else keycodeListener.resetRate()
-            true
-        }
-        KeyEvent.KEYCODE_MINUS -> {
-            keycodeListener.decreaseRate()
-            true
-        }
-        KeyEvent.KEYCODE_B -> {
-            keycodeListener.bookmark()
-            true
+                true
+            }
+            KeyEvent.KEYCODE_EQUALS -> {
+                if (event.isShiftPressed) {
+                    keycodeListener.increaseRate()
+                } else keycodeListener.resetRate()
+                true
+            }
+            KeyEvent.KEYCODE_MINUS -> {
+                keycodeListener.decreaseRate()
+                true
+            }
+            KeyEvent.KEYCODE_B -> {
+                keycodeListener.bookmark()
+                true
+            }
+            else -> false
         }
-        else -> false
     }
 }
 
@@ -108,6 +112,13 @@ class PlayerKeyListenerDelegate(private val keycodeListener: KeycodeListener) {
  * Interface describing the methods that can be triggered by key events
  */
 interface KeycodeListener {
+
+    /**
+     * Get the readiness state of the callee. I not ready, no event will be triggered
+     * @return true if the callee is ready
+     */
+    fun isReady(): Boolean
+
     /**
      * Opens the advanced options menu
      */
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 3ad9d539c..5ac014957 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
@@ -1190,6 +1190,8 @@ open class VideoPlayerActivity : AppCompatActivity(), PlaybackService.Callback,
         }
     }
 
+    override fun isReady() = true
+
     override fun showAdvancedOptions() {
         if (optionsDelegate == null) service?.let {
             optionsDelegate = PlayerOptionsDelegate(this, it)



More information about the Android mailing list