From abeac90735ffa5d93dbbcf5d184a525282de1295 Mon Sep 17 00:00:00 2001 From: Alexander Capehart Date: Sun, 19 Mar 2023 16:19:29 -0600 Subject: [PATCH] image: use asdeferred Use asDeferred with the ExoPlayer image parser. This is much better compared to using withContext. --- app/build.gradle | 3 +- .../auxio/image/extractor/CoverExtractor.kt | 28 ++++--------------- 2 files changed, 7 insertions(+), 24 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 44213127f..1eb7880a6 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -75,7 +75,8 @@ dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" - implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4' + implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4" + implementation "org.jetbrains.kotlinx:kotlinx-coroutines-guava:1.6.4" // --- SUPPORT --- diff --git a/app/src/main/java/org/oxycblt/auxio/image/extractor/CoverExtractor.kt b/app/src/main/java/org/oxycblt/auxio/image/extractor/CoverExtractor.kt index 78a405501..1c2bb113d 100644 --- a/app/src/main/java/org/oxycblt/auxio/image/extractor/CoverExtractor.kt +++ b/app/src/main/java/org/oxycblt/auxio/image/extractor/CoverExtractor.kt @@ -31,6 +31,7 @@ import java.io.ByteArrayInputStream import java.io.InputStream import javax.inject.Inject import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.guava.asDeferred import kotlinx.coroutines.withContext import org.oxycblt.auxio.image.CoverMode import org.oxycblt.auxio.image.ImageSettings @@ -75,30 +76,11 @@ constructor( } private suspend fun extractExoplayerCover(album: Album): InputStream? { - val future = - MetadataRetriever.retrieveMetadata( - mediaSourceFactory, MediaItem.fromUri(album.songs[0].uri)) - - // future.get is a blocking call that makes us spin until the future is done. - // This is bad for a co-routine, as it prevents cancellation and by extension - // messes with the image loading process and causes annoying bugs. - // To fix this we wrap this around in a withContext call to make it suspend and make - // sure that the runner can do other coroutines. - @Suppress("BlockingMethodInNonBlockingContext") val tracks = - withContext(Dispatchers.Default) { - try { - future.get() - } catch (e: Exception) { - null - } - } - - if (tracks == null || tracks.isEmpty) { - // Unrecognized format. This is expected, as ExoPlayer only supports a - // subset of formats. - return null - } + MetadataRetriever.retrieveMetadata( + mediaSourceFactory, MediaItem.fromUri(album.songs[0].uri)) + .asDeferred() + .await() // The metadata extraction process of ExoPlayer results in a dump of all metadata // it found, which must be iterated through.