Skip to content

Commit 60171f3

Browse files
committed
优化播放器手势处理
- 合并左右屏幕单独处理的逻辑 - 优化安全区域大小 - 修复倍数无法恢复原速度
1 parent 8eae9a0 commit 60171f3

File tree

1 file changed

+72
-78
lines changed

1 file changed

+72
-78
lines changed

player/mobile/src/main/kotlin/dev/aaa1115910/bv/player/mobile/controller/BvPlayerController.kt

Lines changed: 72 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ import androidx.compose.runtime.mutableFloatStateOf
3434
import androidx.compose.runtime.mutableIntStateOf
3535
import androidx.compose.runtime.mutableStateOf
3636
import androidx.compose.runtime.remember
37+
import androidx.compose.runtime.rememberCoroutineScope
38+
import androidx.compose.runtime.rememberUpdatedState
3739
import androidx.compose.runtime.setValue
3840
import androidx.compose.ui.Alignment
3941
import androidx.compose.ui.Modifier
@@ -239,6 +241,7 @@ fun BvPlayerControllerVideoContent(
239241
content: @Composable BoxScope.() -> Unit
240242
) {
241243
val context = LocalContext.current
244+
val scope = rememberCoroutineScope()
242245
val videoPlayerSeekData = LocalVideoPlayerSeekData.current
243246
val videoPlayerStateData = LocalVideoPlayerStateData.current
244247
val videoPlayerConfigData = LocalVideoPlayerConfigData.current
@@ -280,10 +283,10 @@ fun BvPlayerControllerVideoContent(
280283
}
281284
}
282285

283-
val onLongPressEnd: () -> Unit = {
286+
val onLongPressEnd: (speed: Float) -> Unit = { oldSpeed ->
284287
Log.i("BvPlayerController", "Screen long press end")
285288
is2xPlaying = false
286-
onChangeSpeed(1f)
289+
onChangeSpeed(oldSpeed)
287290
}
288291

289292
val onDoubleTap: () -> Unit = {
@@ -370,80 +373,35 @@ fun BvPlayerControllerVideoContent(
370373
) {
371374
Box(
372375
modifier = Modifier
373-
.weight(1f)
374376
.fillMaxSize()
375-
.detectTapAndDragGestures(
377+
.detectPlayerGestures(
378+
enableSafetyArea = isFullScreen,
379+
currentSpeed = videoPlayerConfigData.currentVideoSpeed,
376380
onTap = onTap,
377381
onLongPress = onLongPress,
378382
onLongPressEnd = onLongPressEnd,
379383
onDoubleTap = onDoubleTap,
380-
onVerticalDrag = onMovingBrightness,
381-
onHorizontalDrag = { move, inLeftSafetyArea, _ ->
382-
if (inLeftSafetyArea) {
383-
moveStartInSafetyArea = true
384-
Log.i(
385-
"BvPlayerController",
386-
"Left screen horizon drag start in safety area, ignore it"
387-
)
388-
return@detectTapAndDragGestures
389-
}
390-
onHorizontalDrag(move)
391-
},
392-
onDragEnd = { verticalMove, horizontalMove ->
384+
onVolumeDrag = onMovingVolume,
385+
onBrightnessDrag = onMovingBrightness,
386+
onSeekDrag = onHorizontalDrag,
387+
onDragEnd = { volumeMove, brightnessMove, seekMove ->
393388
Log.i(
394389
"BvPlayerController",
395-
"Left screen drag end: [x=$verticalMove, y=$horizontalMove]"
390+
"screen drag end: [volume=$volumeMove, brightness=$brightnessMove, seek=$seekMove]"
396391
)
397-
if (verticalMove != 0f) {
398-
isMovingBrightness = false
399-
} else {
400-
isMovingSeek = false
401-
if (moveStartInSafetyArea) {
402-
moveStartInSafetyArea = false
403-
return@detectTapAndDragGestures
404-
}
405-
val seekMoveMs = horizontalMove.toLong() * 50
406-
onSeekToPosition(moveStartTime + seekMoveMs)
407-
Log.i("BvPlayerController", "Seek move $seekMoveMs")
408-
}
409-
}
410-
)
411-
) {}
412-
Box(
413-
modifier = Modifier
414-
.weight(1f)
415-
.fillMaxSize()
416-
.detectTapAndDragGestures(
417-
onTap = onTap,
418-
onLongPress = onLongPress,
419-
onLongPressEnd = onLongPressEnd,
420-
onDoubleTap = onDoubleTap,
421-
onVerticalDrag = onMovingVolume,
422-
onHorizontalDrag = { move, _, inRightSafetyArea ->
423-
if (inRightSafetyArea) {
424-
moveStartInSafetyArea = true
425-
Log.i(
426-
"BvPlayerController",
427-
"Right screen horizon drag start in safety area, ignore it"
428-
)
429-
return@detectTapAndDragGestures
430-
}
431-
onHorizontalDrag(move)
432-
},
433-
onDragEnd = { verticalMove, horizontalMove ->
434-
Log.i(
435-
"BvPlayerController",
436-
"Right screen drag end: [x=$verticalMove, y=$horizontalMove]"
437-
)
438-
if (verticalMove != 0f) {
392+
if (volumeMove != 0f) {
439393
isMovingVolume = false
394+
Log.i("BvPlayerController", "Stop move volume")
395+
} else if (brightnessMove != 0f) {
396+
isMovingBrightness = false
397+
Log.i("BvPlayerController", "Stop move brightness")
440398
} else {
441399
isMovingSeek = false
442400
if (moveStartInSafetyArea) {
443401
moveStartInSafetyArea = false
444-
return@detectTapAndDragGestures
402+
return@detectPlayerGestures
445403
}
446-
val seekMoveMs = horizontalMove.toLong() * 50
404+
val seekMoveMs = seekMove.toLong() * 50
447405
onSeekToPosition(moveStartTime + seekMoveMs)
448406
Log.i("BvPlayerController", "Seek move $seekMoveMs")
449407
}
@@ -515,24 +473,32 @@ private fun BvPlayerControllerSettingsContent(
515473
}
516474
}
517475

518-
private fun Modifier.detectTapAndDragGestures(
476+
fun Modifier.detectPlayerGestures(
477+
enableSafetyArea: Boolean,
478+
currentSpeed: Float,
519479
onTap: () -> Unit,
520480
onLongPress: () -> Unit,
521-
onLongPressEnd: () -> Unit,
481+
onLongPressEnd: (speed: Float) -> Unit,
522482
onDoubleTap: () -> Unit,
523-
onVerticalDrag: (move: Float) -> Unit,
524-
onHorizontalDrag: (move: Float, inLeftSafetyArea: Boolean, inRightSafetyArea: Boolean) -> Unit,
525-
onDragEnd: (verticalMove: Float, horizontalMove: Float) -> Unit,
483+
onVolumeDrag: (move: Float) -> Unit,
484+
onBrightnessDrag: (move: Float) -> Unit,
485+
onSeekDrag: (move: Float) -> Unit,
486+
onDragEnd: (volumeMove: Float, brightnessMove: Float, seekMove: Float) -> Unit,
526487
): Modifier = composed {
488+
val currentSpeedState = rememberUpdatedState(currentSpeed)
489+
var oldPlaySpeed by remember { mutableFloatStateOf(1f) }
527490
var componentWidth by remember { mutableIntStateOf(0) }
491+
var componentHeight by remember { mutableIntStateOf(0) }
528492
val horizontalSafetyArea = 0.1f
493+
val verticalSafetyArea = 0.2f
529494
var determinedDirection by remember { mutableStateOf(false) }
530495
var isHorizontal by remember { mutableStateOf(false) }
531496
var horizontalPointMove by remember { mutableFloatStateOf(0f) }
532497
var verticalPointMove by remember { mutableFloatStateOf(0f) }
533-
var inLeftSafetyArea by remember { mutableStateOf(false) }
534-
var inRightSafetyArea by remember { mutableStateOf(false) }
498+
var inSafetyArea by remember { mutableStateOf(false) }
535499
var longPressing by remember { mutableStateOf(false) }
500+
var isMovingVolume by remember { mutableStateOf(false) }
501+
var isMovingBrightness by remember { mutableStateOf(false) }
536502

537503
pointerInput(Unit) {
538504
detectTapGestures(
@@ -542,6 +508,7 @@ private fun Modifier.detectTapAndDragGestures(
542508
},
543509
onLongPress = {
544510
onLongPress()
511+
oldPlaySpeed = currentSpeedState.value
545512
longPressing = true
546513
},
547514
onDoubleTap = {
@@ -550,33 +517,56 @@ private fun Modifier.detectTapAndDragGestures(
550517
},
551518
onPress = {
552519
tryAwaitRelease()
553-
if (longPressing) onLongPressEnd()
520+
if (longPressing) onLongPressEnd(oldPlaySpeed)
554521
longPressing = false
555522
}
556523
)
557524
}
558525
.onSizeChanged { size ->
559526
componentWidth = size.width
527+
componentHeight = size.height
560528
}
561-
.pointerInput(Unit) {
529+
.pointerInput(enableSafetyArea) {
562530
if (longPressing) return@pointerInput
531+
563532
detectDragGestures(
564533
onDragStart = {
565-
println("Drag start: $it, safety left x range: [0, ${componentWidth * horizontalSafetyArea}], safety right x range: [${componentWidth * (1 - horizontalSafetyArea)}, ${componentWidth}]")
566-
inLeftSafetyArea = it.x < componentWidth * horizontalSafetyArea
567-
inRightSafetyArea = it.x > componentWidth * (1 - horizontalSafetyArea)
534+
println("Drag start: $it, safety x range: (${componentWidth * horizontalSafetyArea}, ${componentWidth * (1 - horizontalSafetyArea)}), safety y range: (${componentHeight * verticalSafetyArea}, ${componentHeight * (1 - verticalSafetyArea)})")
535+
val inHorizontalSafetyArea =
536+
it.x > componentWidth * horizontalSafetyArea * 0.5f && it.x < componentWidth * (1 - horizontalSafetyArea * 0.5f)
537+
val inVerticalSafetyArea =
538+
it.y > componentHeight * verticalSafetyArea * 0.5f && it.y < componentHeight * (1 - verticalSafetyArea * 0.5f)
539+
inSafetyArea =
540+
inHorizontalSafetyArea && inVerticalSafetyArea || !enableSafetyArea
541+
if (!inSafetyArea) return@detectDragGestures
542+
543+
if (it.x < componentWidth * 0.5f) {
544+
isMovingBrightness = true
545+
} else if (it.x >= componentWidth * 0.5f) {
546+
isMovingVolume = true
547+
}
568548
},
569549
onDragEnd = {
550+
if (!inSafetyArea) return@detectDragGestures
551+
570552
if (isHorizontal) {
571-
onDragEnd(0f, horizontalPointMove)
553+
onDragEnd(0f, 0f, horizontalPointMove)
572554
} else {
573-
onDragEnd(verticalPointMove, 0f)
555+
if (isMovingVolume) {
556+
onDragEnd(verticalPointMove, 0f, 0f)
557+
} else if (isMovingBrightness) {
558+
onDragEnd(0f, verticalPointMove, 0f)
559+
}
574560
}
561+
575562
horizontalPointMove = 0f
576563
verticalPointMove = 0f
577564
determinedDirection = false
565+
isMovingVolume = false
566+
isMovingBrightness = false
578567
}
579568
) { _, dragAmount ->
569+
if (!inSafetyArea) return@detectDragGestures
580570
horizontalPointMove += dragAmount.x
581571
verticalPointMove += dragAmount.y
582572
if (!determinedDirection) {
@@ -590,9 +580,13 @@ private fun Modifier.detectTapAndDragGestures(
590580
}
591581
if (determinedDirection) {
592582
if (isHorizontal) {
593-
onHorizontalDrag(horizontalPointMove, inLeftSafetyArea, inRightSafetyArea)
583+
onSeekDrag(horizontalPointMove)
594584
} else {
595-
onVerticalDrag(verticalPointMove)
585+
if (isMovingVolume) {
586+
onVolumeDrag(verticalPointMove)
587+
} else if (isMovingBrightness) {
588+
onBrightnessDrag(verticalPointMove)
589+
}
596590
}
597591
}
598592
}

0 commit comments

Comments
 (0)