From e9778d6febdc57a516fd58e2b1e35bf2a00a227c Mon Sep 17 00:00:00 2001 From: Valere Date: Wed, 8 Jul 2020 22:41:17 +0200 Subject: [PATCH] Video stop/resume when paging or bg/fg --- .../AttachmentViewerActivity.kt | 9 +++++++ .../attachmentviewer/AttachmentsAdapter.kt | 13 ++++++++++ .../riotx/attachmentviewer/VideoViewHolder.kt | 25 ++++++++++++++++++- 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/attachment-viewer/src/main/java/im/vector/riotx/attachmentviewer/AttachmentViewerActivity.kt b/attachment-viewer/src/main/java/im/vector/riotx/attachmentviewer/AttachmentViewerActivity.kt index 99a90eb033..2a83ab21c7 100644 --- a/attachment-viewer/src/main/java/im/vector/riotx/attachmentviewer/AttachmentViewerActivity.kt +++ b/attachment-viewer/src/main/java/im/vector/riotx/attachmentviewer/AttachmentViewerActivity.kt @@ -143,6 +143,15 @@ abstract class AttachmentViewerActivity : AppCompatActivity(), AttachmentEventLi overlayView = attachmentsAdapter.attachmentSourceProvider?.overlayViewAtPosition(this@AttachmentViewerActivity, position) } + override fun onPause() { + attachmentsAdapter.onPause(currentPosition) + super.onPause() + } + + override fun onResume() { + super.onResume() + attachmentsAdapter.onResume(currentPosition) + } override fun dispatchTouchEvent(ev: MotionEvent): Boolean { // The zoomable view is configured to disallow interception when image is zoomed diff --git a/attachment-viewer/src/main/java/im/vector/riotx/attachmentviewer/AttachmentsAdapter.kt b/attachment-viewer/src/main/java/im/vector/riotx/attachmentviewer/AttachmentsAdapter.kt index 333a1b3625..d1929f271e 100644 --- a/attachment-viewer/src/main/java/im/vector/riotx/attachmentviewer/AttachmentsAdapter.kt +++ b/attachment-viewer/src/main/java/im/vector/riotx/attachmentviewer/AttachmentsAdapter.kt @@ -28,6 +28,8 @@ abstract class BaseViewHolder constructor(itemView: View) : open fun onRecycled() {} open fun onAttached() {} open fun onDetached() {} + open fun entersBackground() {} + open fun entersForeground() {} open fun onSelected(selected: Boolean) {} } @@ -121,6 +123,17 @@ class AttachmentsAdapter() : RecyclerView.Adapter() { return false } + + fun onPause(position: Int) { + val holder = recyclerView?.findViewHolderForAdapterPosition(position) as? BaseViewHolder + holder?.entersBackground() + } + + fun onResume(position: Int) { + val holder = recyclerView?.findViewHolderForAdapterPosition(position) as? BaseViewHolder + holder?.entersForeground() + + } // override fun getItemCount(): Int { // return 8 // } diff --git a/attachment-viewer/src/main/java/im/vector/riotx/attachmentviewer/VideoViewHolder.kt b/attachment-viewer/src/main/java/im/vector/riotx/attachmentviewer/VideoViewHolder.kt index 38b656559e..ea5fed1acb 100644 --- a/attachment-viewer/src/main/java/im/vector/riotx/attachmentviewer/VideoViewHolder.kt +++ b/attachment-viewer/src/main/java/im/vector/riotx/attachmentviewer/VideoViewHolder.kt @@ -26,6 +26,7 @@ import androidx.core.view.isVisible import io.reactivex.Observable import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.disposables.Disposable +import kotlinx.coroutines.selects.select import java.io.File import java.lang.ref.WeakReference import java.util.concurrent.TimeUnit @@ -39,6 +40,7 @@ class VideoViewHolder constructor(itemView: View) : private var isSelected = false private var mVideoPath: String? = null private var progressDisposable: Disposable? = null + private var progress: Int = 0 var eventListener: WeakReference? = null @@ -89,12 +91,30 @@ class VideoViewHolder constructor(itemView: View) : } } + override fun entersBackground() { + if (videoView.isPlaying) { + progress = videoView.currentPosition + progressDisposable?.dispose() + progressDisposable = null + videoView.stopPlayback() + videoView.pause() + } + + } + + override fun entersForeground() { + onSelected(isSelected) + } + override fun onSelected(selected: Boolean) { if (!selected) { if (videoView.isPlaying) { + progress = videoView.currentPosition videoView.stopPlayback() progressDisposable?.dispose() progressDisposable = null + } else { + progress = 0 } } else { if (mVideoPath != null) { @@ -125,9 +145,12 @@ class VideoViewHolder constructor(itemView: View) : videoView.setVideoPath(mVideoPath) videoView.start() + if (progress > 0) { + videoView.seekTo(progress) + } } override fun bind(attachmentInfo: AttachmentInfo) { - Log.v("FOO", "") + progress = 0 } }