修复: dispatchTouchEvent替代OnTouchListener,解决点击视频无法暂停问题; controllerHideOnTouch=false解决暂停后进度条不显示问题
This commit is contained in:
@@ -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 = event.x
|
touchStartX = ev.x
|
||||||
touchStartY = event.y
|
touchStartY = ev.y
|
||||||
isAdjusting = false
|
isAdjusting = false
|
||||||
isControllerTouch = event.y > view.height * 0.8f
|
val halfWidth = window.decorView.width / 2
|
||||||
if (isControllerTouch) return@OnTouchListener false
|
|
||||||
val halfWidth = view.width / 2
|
|
||||||
isBrightnessMode = touchStartX < halfWidth
|
isBrightnessMode = touchStartX < halfWidth
|
||||||
startBrightness = window.attributes.screenBrightness
|
startBrightness = window.attributes.screenBrightness
|
||||||
if (startBrightness < 0) startBrightness = 0.5f
|
if (startBrightness < 0) startBrightness = 0.5f
|
||||||
startVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC)
|
startVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC)
|
||||||
true
|
|
||||||
}
|
}
|
||||||
MotionEvent.ACTION_MOVE -> {
|
MotionEvent.ACTION_MOVE -> {
|
||||||
val deltaY = touchStartY - event.y
|
val deltaY = touchStartY - ev.y
|
||||||
if (kotlin.math.abs(event.y - touchStartY) > 10f) {
|
if (kotlin.math.abs(ev.y - touchStartY) > 20f) {
|
||||||
isAdjusting = true
|
isAdjusting = true
|
||||||
}
|
}
|
||||||
if (isAdjusting) {
|
if (isAdjusting) {
|
||||||
if (isBrightnessMode) {
|
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.screenBrightness = newBrightness
|
||||||
window.attributes = window.attributes
|
window.attributes = window.attributes
|
||||||
showIndicator("亮度", (newBrightness * 100).toInt())
|
showIndicator("亮度", (newBrightness * 100).toInt())
|
||||||
} else {
|
} else {
|
||||||
val range = maxVolume.toFloat()
|
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)
|
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, newVolume, 0)
|
||||||
showIndicator("音量", (newVolume * 100 / maxVolume))
|
showIndicator("音量", (newVolume * 100 / maxVolume))
|
||||||
}
|
}
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
true
|
|
||||||
}
|
}
|
||||||
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
|
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
|
||||||
val movedFar = kotlin.math.abs(event.x - touchStartX) > 20f || kotlin.math.abs(event.y - touchStartY) > 20f
|
if (!isAdjusting) {
|
||||||
if (!isAdjusting && !movedFar) {
|
val movedFar = kotlin.math.abs(ev.x - touchStartX) > 30f || kotlin.math.abs(ev.y - touchStartY) > 30f
|
||||||
toggleControls()
|
if (!movedFar) {
|
||||||
false
|
val controlPanelTop = controlPanel.top
|
||||||
} else {
|
if (controlPanel.visibility != View.VISIBLE || ev.y < controlPanelTop) {
|
||||||
true
|
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) {
|
private fun showIndicator(label: String, percent: Int) {
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user