Basic video seekTo support

This commit is contained in:
Valere 2020-07-09 08:59:06 +02:00
parent e24d5b3ca4
commit bf2d937ad6
6 changed files with 44 additions and 10 deletions

View File

@ -27,4 +27,5 @@ interface AttachmentEventListener {
sealed class AttachmentCommands {
object PauseVideo : AttachmentCommands()
object StartVideo : AttachmentCommands()
data class SeekTo(val percentProgress: Int) : AttachmentCommands()
}

View File

@ -303,10 +303,9 @@ abstract class AttachmentViewerActivity : AppCompatActivity(), AttachmentEventLi
finish()
}
public fun handle(commands: AttachmentCommands) {
(attachmentsAdapter.recyclerView?.findViewHolderForAdapterPosition(currentPosition) as? BaseViewHolder)?.let {
it.handleCommand(commands)
}
fun handle(commands: AttachmentCommands) {
(attachmentsAdapter.recyclerView?.findViewHolderForAdapterPosition(currentPosition) as? BaseViewHolder)
?.handleCommand(commands)
}
private fun hideSystemUI() {

View File

@ -162,6 +162,13 @@ class VideoViewHolder constructor(itemView: View) :
wasPaused = true
videoView.pause()
}
is AttachmentCommands.SeekTo -> {
val duration = videoView.duration
if (duration > 0) {
val seekDuration = duration * (commands.percentProgress / 100f)
videoView.seekTo(seekDuration.toInt())
}
}
}
}

View File

@ -36,6 +36,7 @@ class AttachmentOverlayView @JvmOverloads constructor(
var onShareCallback: (() -> Unit)? = null
var onBack: (() -> Unit)? = null
var onPlayPause: ((play: Boolean) -> Unit)? = null
var videoSeekTo: ((progress: Int) -> Unit)? = null
private val counterTextView: TextView
private val infoTextView: TextView
@ -47,6 +48,8 @@ class AttachmentOverlayView @JvmOverloads constructor(
val videoControlsGroup: Group
var suspendSeekBarUpdate = false
init {
View.inflate(context, R.layout.merge_image_attachment_overlay, this)
setBackgroundColor(Color.TRANSPARENT)
@ -56,8 +59,6 @@ class AttachmentOverlayView @JvmOverloads constructor(
videoControlsGroup = findViewById(R.id.overlayVideoControlsGroup)
overlayPlayPauseButton = findViewById(R.id.overlayPlayPauseButton)
overlaySeekBar = findViewById(R.id.overlaySeekBar)
overlaySeekBar.isEnabled = false
findViewById<ImageView>(R.id.overlayBackButton).setOnClickListener {
onBack?.invoke()
}
@ -67,6 +68,22 @@ class AttachmentOverlayView @JvmOverloads constructor(
findViewById<ImageView>(R.id.overlayPlayPauseButton).setOnClickListener {
onPlayPause?.invoke(!isPlaying)
}
overlaySeekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
if (fromUser) {
videoSeekTo?.invoke(progress)
}
}
override fun onStartTrackingTouch(seekBar: SeekBar?) {
suspendSeekBarUpdate = true
}
override fun onStopTrackingTouch(seekBar: SeekBar?) {
suspendSeekBarUpdate = false
}
})
}
fun updateWith(counter: String, senderInfo: String) {
@ -78,10 +95,12 @@ class AttachmentOverlayView @JvmOverloads constructor(
when (event) {
is AttachmentEvents.VideoEvent -> {
overlayPlayPauseButton.setImageResource(if (!event.isPlaying) R.drawable.ic_play_arrow else R.drawable.ic_pause)
val safeDuration = (if (event.duration == 0) 100 else event.duration).toFloat()
val percent = ((event.progress / safeDuration) * 100f).toInt().coerceAtMost(100)
isPlaying = event.isPlaying
overlaySeekBar.progress = percent
if (!suspendSeekBarUpdate) {
val safeDuration = (if (event.duration == 0) 100 else event.duration).toFloat()
val percent = ((event.progress / safeDuration) * 100f).toInt().coerceAtMost(100)
isPlaying = event.isPlaying
overlaySeekBar.progress = percent
}
}
}
}

View File

@ -58,6 +58,7 @@ class RoomAttachmentProvider(
fun onDismissTapped()
fun onShareTapped()
fun onPlayPause(play: Boolean)
fun videoSeekTo(percent: Int)
}
var interactionListener: InteractionListener? = null
@ -201,6 +202,9 @@ class RoomAttachmentProvider(
overlayView?.onPlayPause = { play ->
interactionListener?.onPlayPause(play)
}
overlayView?.videoSeekTo = { percent ->
interactionListener?.videoSeekTo(percent)
}
}
val item = attachments[position]
val dateString = item.root.localDateTime().let {

View File

@ -247,6 +247,10 @@ class VectorAttachmentViewerActivity : AttachmentViewerActivity(), RoomAttachmen
handle(if (play) AttachmentCommands.StartVideo else AttachmentCommands.PauseVideo)
}
override fun videoSeekTo(percent: Int) {
handle(AttachmentCommands.SeekTo(percent))
}
override fun onShareTapped() {
// Share
eventList?.get(currentPosition)?.let { timelineEvent ->