feat: add play/pause functionality to onDoubleTap (#450)

* feat: Add seek and play/pause functionality to onDoubleTap

This patch enhances the `onDoubleTap` method within the media player
component. It introduces a split-screen layout where the player view is
divided into three equal areas (in the ratio of 2:1:2).

1. Leftmost Area: When double-tapped, it seeks the media playback
backward by the defined seek increment
(`appPreferences.playerSeekBackIncrement`).

2. Middle Area: A double-tap in this area toggles the play/pause state
of the player. If the player is currently playing, it will be paused,
and if it's paused, it will be resumed.

3. Rightmost Area: When double-tapped, it seeks the media playback
forward by the specified seek increment
(`appPreferences.playerSeekForwardIncrement`).

* refactor: inline some variables and put x position inside when statement

---------

Co-authored-by: Jarne Demeulemeester <jarnedemeulemeester@gmail.com>
This commit is contained in:
Daniel Jacob Chittoor 2023-07-29 16:59:05 +05:30 committed by GitHub
parent 0efec85953
commit 57c1e85b11
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -68,13 +68,30 @@ class PlayerGestureHelper(
override fun onDoubleTap(e: MotionEvent): Boolean {
// Disables double tap gestures if view is locked
if (isControlsLocked) return false
val viewCenterX = playerView.measuredWidth / 2
val viewWidth = playerView.measuredWidth
val areaWidth = viewWidth / 5 // Divide the view into 5 parts: 2:1:2
val currentPos = playerView.player?.currentPosition ?: 0
if (e.x.toInt() > viewCenterX) {
playerView.player?.seekTo(currentPos + appPreferences.playerSeekForwardIncrement)
} else {
playerView.player?.seekTo((currentPos - appPreferences.playerSeekBackIncrement).coerceAtLeast(0))
// Define the areas and their boundaries
val leftmostAreaStart = 0
val middleAreaStart = areaWidth * 2
val rightmostAreaStart = middleAreaStart + areaWidth
when (e.x.toInt()) {
in leftmostAreaStart until middleAreaStart -> {
// Tapped on the leftmost area (seek backward)
playerView.player?.seekTo((currentPos - appPreferences.playerSeekBackIncrement).coerceAtLeast(0))
}
in middleAreaStart until rightmostAreaStart -> {
// Tapped on the middle area (toggle pause/unpause)
playerView.player?.playWhenReady = !playerView.player?.playWhenReady!!
}
in rightmostAreaStart until viewWidth -> {
// Tapped on the rightmost area (seek forward)
playerView.player?.seekTo(currentPos + appPreferences.playerSeekForwardIncrement)
}
}
return true
}