I am creating a Gallery application and i have a Tab that shows all the videos of a folder. The layout must show a preview of the video and the duration inside a textview upon the preview.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp">
<ImageView
android:id="@+id/video_preview"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_margin="4dp"
android:scaleType="centerCrop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/logo"
tools:ignore="ContentDescription" />
<TextView
android:id="@+id/video_duration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="4dp"
android:layout_marginBottom="4dp"
android:text="00:00"
android:background="@drawable/primary_background"
android:paddingHorizontal="4dp"
android:paddingVertical="2dp"
android:textColor="@color/colorWhite"
android:textSize="10sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
The only thing that i have from the data is the AttachmentItem which holds the path of the Video.
class VideoViewHolder internal constructor(itemView: View) : GalleryItemVewHolder(itemView) {
private val videoPreview: ImageView = itemView.findViewById(R.id.video_preview)
private val selectedBox: AppCompatCheckBox = itemView.findViewById(R.id.selectedBox)
private val videoDuration: TextView = itemView.findViewById(R.id.video_duration)
fun bind(
video: FileGalleryUi.VideoItem,
videoCallback: RecyclerCallback.GalleryCallback,
notifyCallback: (file: FileGalleryUi, pos: Int) -> Unit
) {
val originalFile = video.attachmentItem.toFile(itemView.context)
Glide.with(itemView.context)
.load(originalFile)
.error(ContextCompat.getDrawable(itemView.context, R.drawable.ic_gallery))
.into(videoPreview)
// Below line makes the recycler to lag when it scrolls. If i comment out the scrolling become very smooth as expected.
tryOrNull {
MediaMetadataRetriever().apply {
setDataSource(originalFile.absolutePath)
}.also { mmr ->
videoDuration.text = DateUtils.millisToDuration(
mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)?.toLong()
)
videoDuration.visibility = View.VISIBLE
}
} ?: run {
videoDuration.visibility = View.GONE
}
}
companion object {
fun from(parent: ViewGroup): VideoViewHolder =
VideoViewHolder(
LayoutInflater.from(parent.context).inflate(R.layout.video_gallery_item, parent, false)
)
}
}
Its obvious to me that if i comment out the Duration setup block (from the tryOrNull) the recycler scrolls very smooth and if i keep it uncommented then it lags when scroll. Is there a better and more performant way to get the duration without block the UI?