Skip to content

Instantly share code, notes, and snippets.

@tizisdeepan
Created August 22, 2019 12:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tizisdeepan/f052b8fdf5ddd6bcd2af4bfd471ef338 to your computer and use it in GitHub Desktop.
Save tizisdeepan/f052b8fdf5ddd6bcd2af4bfd471ef338 to your computer and use it in GitHub Desktop.
BackgroundExecutor.execute(object : BackgroundExecutor.Task("", 0L, "") {
override fun execute() {
try {
val threshold = 11
val thumbnailList = LongSparseArray<Bitmap>()
val mediaMetadataRetriever = MediaMetadataRetriever()
mediaMetadataRetriever.setDataSource(context, mVideoUri)
val videoLengthInMs = (Integer.parseInt(mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)) * 1000).toLong()
val frameHeight = mHeightView
val initialBitmap = mediaMetadataRetriever.getFrameAtTime(0, MediaMetadataRetriever.OPTION_CLOSEST_SYNC)
val frameWidth = ((initialBitmap.width.toFloat() / initialBitmap.height.toFloat()) * frameHeight.toFloat()).toInt()
var numThumbs = ceil((viewWidth.toFloat() / frameWidth)).toInt()
if (numThumbs < threshold) numThumbs = threshold
val cropWidth = viewWidth / threshold
val interval = videoLengthInMs / numThumbs
for (i in 0 until numThumbs) {
var bitmap = mediaMetadataRetriever.getFrameAtTime(i * interval, MediaMetadataRetriever.OPTION_CLOSEST_SYNC)
if (bitmap != null) {
try {
bitmap = Bitmap.createScaledBitmap(bitmap, frameWidth, frameHeight, false)
bitmap = Bitmap.createBitmap(bitmap, 0, 0, cropWidth, bitmap.height)
} catch (e: Exception) {
e.printStackTrace()
}
thumbnailList.put(i.toLong(), bitmap)
}
}
mediaMetadataRetriever.release()
returnBitmaps(thumbnailList)
} catch (e: Throwable) {
Thread.getDefaultUncaughtExceptionHandler().uncaughtException(Thread.currentThread(), e)
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment