Skip to content

Commit

Permalink
[Feature] Update Coil and apply MediaMetadataRetriever BitmapParams fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhanghai committed Oct 2, 2022
1 parent cfb77f0 commit 477e073
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ dependencies {
implementation 'dev.rikka.rikkax.preference:simplemenu-preference:1.0.3'
implementation 'dev.rikka.shizuku:api:12.0.0'
implementation 'eu.agno3.jcifs:jcifs-ng:2.1.6'
def coil_version = '2.2.1'
def coil_version = '2.2.2'
implementation "io.coil-kt:coil:$coil_version"
implementation "io.coil-kt:coil-gif:$coil_version"
implementation "io.coil-kt:coil-svg:$coil_version"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import coil.fetch.Fetcher
import coil.request.Options
import coil.request.videoFrameOption
import coil.request.videoFramePercent
import me.zhanghai.android.files.compat.getFrameAtTimeCompat
import me.zhanghai.android.files.compat.getScaledFrameAtTimeCompat
import java.util.concurrent.TimeUnit
import kotlin.math.roundToInt
import kotlin.math.roundToLong
Expand Down Expand Up @@ -66,6 +68,11 @@ class VideoFrameFetcher(
)
val frameOption = options.parameters.videoFrameOption()
?: MediaMetadataRetriever.OPTION_CLOSEST_SYNC
val bitmapParams = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
MediaMetadataRetriever.BitmapParams().apply { preferredConfig = options.config }
} else {
null
}
val outBitmap = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1
&& srcWidth > 0 && srcHeight > 0) {
val dstWidth = options.size.widthPx(options.scale) { srcWidth }
Expand All @@ -84,9 +91,11 @@ class VideoFrameFetcher(
}
val width = (scale * srcWidth).roundToInt()
val height = (scale * srcHeight).roundToInt()
retriever.getScaledFrameAtTime(frameMicros, frameOption, width, height)
retriever.getScaledFrameAtTimeCompat(
frameMicros, frameOption, width, height, bitmapParams
)
} else {
retriever.getFrameAtTime(frameMicros, frameOption)?.also {
retriever.getFrameAtTimeCompat(frameMicros, frameOption, bitmapParams)?.also {
srcWidth = it.width
srcHeight = it.height
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package me.zhanghai.android.files.compat

import android.graphics.Bitmap
import android.media.MediaMetadataRetriever
import android.os.Build
import androidx.annotation.RequiresApi
Expand All @@ -17,6 +18,31 @@ val KClass<MediaMetadataRetriever>.METADATA_KEY_SAMPLERATE: Int
@RequiresApi(Build.VERSION_CODES.Q)
get() = 38

fun MediaMetadataRetriever.getFrameAtTimeCompat(
timeUs: Long,
option: Int,
params: MediaMetadataRetriever.BitmapParams?
): Bitmap? =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && params != null) {
getFrameAtTime(timeUs, option, params)
} else {
getFrameAtTime(timeUs, option)
}

@RequiresApi(Build.VERSION_CODES.O_MR1)
fun MediaMetadataRetriever.getScaledFrameAtTimeCompat(
timeUs: Long,
option: Int,
dstWidth: Int,
dstHeight: Int,
params: MediaMetadataRetriever.BitmapParams?
): Bitmap? =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && params != null) {
getScaledFrameAtTime(timeUs, option, dstWidth, dstHeight, params)
} else {
getScaledFrameAtTime(timeUs, option, dstWidth, dstHeight)
}

@OptIn(ExperimentalContracts::class)
inline fun <R> MediaMetadataRetriever.use(block: (MediaMetadataRetriever) -> R): R {
contract {
Expand Down

0 comments on commit 477e073

Please sign in to comment.