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

View File

@@ -256,11 +256,22 @@ class SearchFragment : Fragment() {
return 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 { val chip = Button(requireContext()).apply {
text = item.keyword text = item.keyword
setTextColor(ContextCompat.getColor(requireContext(), R.color.text_primary)) setTextColor(android.graphics.Color.WHITE)
setBackgroundResource(R.drawable.history_chip_selector) background = android.graphics.drawable.GradientDrawable().apply {
setColor(bgColor)
cornerRadius = 20f
}
textSize = 14f textSize = 14f
setPadding(24, 8, 24, 8) setPadding(24, 8, 24, 8)
isFocusable = true isFocusable = true