修复: dispatchTouchEvent替代OnTouchListener,解决点击视频无法暂停问题; controllerHideOnTouch=false解决暂停后进度条不显示问题

This commit is contained in:
xiaji
2026-05-31 18:22:30 +08:00
parent 06e55ccb26
commit 1f363e74fa
2 changed files with 53 additions and 48 deletions

View File

@@ -76,7 +76,6 @@ class PlayerActivity : AppCompatActivity() {
private var touchStartX = 0f
private var isAdjusting = false
private var isBrightnessMode = false
private var isControllerTouch = false
private var startBrightness = 0f
private var startVolume = 0
private var maxVolume = 0
@@ -118,12 +117,12 @@ class PlayerActivity : AppCompatActivity() {
initExoPlayer()
loadSources(detailUrl)
setupTouchListeners()
}
private fun initExoPlayer() {
exoPlayer = ExoPlayer.Builder(this).build().also { player ->
playerView.player = player
playerView.controllerHideOnTouch = false
player.addListener(object : Player.Listener {
override fun onPlayWhenReadyChanged(playWhenReady: Boolean, reason: Int) {
if (playWhenReady) {
@@ -383,56 +382,51 @@ class PlayerActivity : AppCompatActivity() {
showLoading(false)
}
private fun setupTouchListeners() {
val touchListener = View.OnTouchListener { view, event ->
when (event.action) {
override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
when (ev.action) {
MotionEvent.ACTION_DOWN -> {
touchStartX = event.x
touchStartY = event.y
touchStartX = ev.x
touchStartY = ev.y
isAdjusting = false
isControllerTouch = event.y > view.height * 0.8f
if (isControllerTouch) return@OnTouchListener false
val halfWidth = view.width / 2
val halfWidth = window.decorView.width / 2
isBrightnessMode = touchStartX < halfWidth
startBrightness = window.attributes.screenBrightness
if (startBrightness < 0) startBrightness = 0.5f
startVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC)
true
}
MotionEvent.ACTION_MOVE -> {
val deltaY = touchStartY - event.y
if (kotlin.math.abs(event.y - touchStartY) > 10f) {
val deltaY = touchStartY - ev.y
if (kotlin.math.abs(ev.y - touchStartY) > 20f) {
isAdjusting = true
}
if (isAdjusting) {
if (isBrightnessMode) {
val newBrightness = (startBrightness + deltaY / view.height).coerceIn(0.01f, 1.0f)
val newBrightness = (startBrightness + deltaY / window.decorView.height).coerceIn(0.01f, 1.0f)
window.attributes.screenBrightness = newBrightness
window.attributes = window.attributes
showIndicator("亮度", (newBrightness * 100).toInt())
} else {
val range = maxVolume.toFloat()
val newVolume = (startVolume + (deltaY / view.height) * range).toInt().coerceIn(0, maxVolume)
val newVolume = (startVolume + (deltaY / window.decorView.height) * range).toInt().coerceIn(0, maxVolume)
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, newVolume, 0)
showIndicator("音量", (newVolume * 100 / maxVolume))
}
return true
}
true
}
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
val movedFar = kotlin.math.abs(event.x - touchStartX) > 20f || kotlin.math.abs(event.y - touchStartY) > 20f
if (!isAdjusting && !movedFar) {
toggleControls()
false
} else {
true
if (!isAdjusting) {
val movedFar = kotlin.math.abs(ev.x - touchStartX) > 30f || kotlin.math.abs(ev.y - touchStartY) > 30f
if (!movedFar) {
val controlPanelTop = controlPanel.top
if (controlPanel.visibility != View.VISIBLE || ev.y < controlPanelTop) {
exoPlayer?.let { it.playWhenReady = !it.playWhenReady }
}
}
else -> false
}
}
playerView.setOnTouchListener(touchListener)
playerWebView.setOnTouchListener(touchListener)
}
return super.dispatchTouchEvent(ev)
}
private fun showIndicator(label: String, percent: Int) {

View File

@@ -256,11 +256,22 @@ class SearchFragment : Fragment() {
return
}
for (item in list) {
val chipColors = intArrayOf(
0xFF1A73E8.toInt(), 0xFF34A853.toInt(), 0xFFEA4335.toInt(),
0xFFFBBC04.toInt(), 0xFF8E24AA.toInt(), 0xFF00ACC1.toInt(),
0xFFFF6F00.toInt(), 0xFF43A047.toInt(), 0xFF1E88E5.toInt(),
0xFFE53935.toInt()
)
for ((index, item) in list.withIndex()) {
val bgColor = chipColors[index % chipColors.size]
val chip = Button(requireContext()).apply {
text = item.keyword
setTextColor(ContextCompat.getColor(requireContext(), R.color.text_primary))
setBackgroundResource(R.drawable.history_chip_selector)
setTextColor(android.graphics.Color.WHITE)
background = android.graphics.drawable.GradientDrawable().apply {
setColor(bgColor)
cornerRadius = 20f
}
textSize = 14f
setPadding(24, 8, 24, 8)
isFocusable = true