Video stop/resume when paging or bg/fg

This commit is contained in:
Valere 2020-07-08 22:41:17 +02:00
parent 8c4c909f44
commit e9778d6feb
3 changed files with 46 additions and 1 deletions
attachment-viewer/src/main/java/im/vector/riotx/attachmentviewer

View file

@ -143,6 +143,15 @@ abstract class AttachmentViewerActivity : AppCompatActivity(), AttachmentEventLi
overlayView = attachmentsAdapter.attachmentSourceProvider?.overlayViewAtPosition(this@AttachmentViewerActivity, position) 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 { override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
// The zoomable view is configured to disallow interception when image is zoomed // The zoomable view is configured to disallow interception when image is zoomed

View file

@ -28,6 +28,8 @@ abstract class BaseViewHolder constructor(itemView: View) :
open fun onRecycled() {} open fun onRecycled() {}
open fun onAttached() {} open fun onAttached() {}
open fun onDetached() {} open fun onDetached() {}
open fun entersBackground() {}
open fun entersForeground() {}
open fun onSelected(selected: Boolean) {} open fun onSelected(selected: Boolean) {}
} }
@ -121,6 +123,17 @@ class AttachmentsAdapter() : RecyclerView.Adapter<BaseViewHolder>() {
return false 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 { // override fun getItemCount(): Int {
// return 8 // return 8
// } // }

View file

@ -26,6 +26,7 @@ import androidx.core.view.isVisible
import io.reactivex.Observable import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable import io.reactivex.disposables.Disposable
import kotlinx.coroutines.selects.select
import java.io.File import java.io.File
import java.lang.ref.WeakReference import java.lang.ref.WeakReference
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
@ -39,6 +40,7 @@ class VideoViewHolder constructor(itemView: View) :
private var isSelected = false private var isSelected = false
private var mVideoPath: String? = null private var mVideoPath: String? = null
private var progressDisposable: Disposable? = null private var progressDisposable: Disposable? = null
private var progress: Int = 0
var eventListener: WeakReference<AttachmentEventListener>? = null var eventListener: WeakReference<AttachmentEventListener>? = 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) { override fun onSelected(selected: Boolean) {
if (!selected) { if (!selected) {
if (videoView.isPlaying) { if (videoView.isPlaying) {
progress = videoView.currentPosition
videoView.stopPlayback() videoView.stopPlayback()
progressDisposable?.dispose() progressDisposable?.dispose()
progressDisposable = null progressDisposable = null
} else {
progress = 0
} }
} else { } else {
if (mVideoPath != null) { if (mVideoPath != null) {
@ -125,9 +145,12 @@ class VideoViewHolder constructor(itemView: View) :
videoView.setVideoPath(mVideoPath) videoView.setVideoPath(mVideoPath)
videoView.start() videoView.start()
if (progress > 0) {
videoView.seekTo(progress)
}
} }
override fun bind(attachmentInfo: AttachmentInfo) { override fun bind(attachmentInfo: AttachmentInfo) {
Log.v("FOO", "") progress = 0
} }
} }