Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: square crop albumart option #97

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ class MainActivity : ComponentActivity() {
this, playerConfig = PlayerConfig(
interceptPlayerActionsTriggeredExternally = true,
handleAudioBecomingNoisy = true,
handleAudioFocus = true
handleAudioFocus = true,
// squareCropAlbumArt = true,
)
)
player.add(tracks)
Expand Down Expand Up @@ -173,6 +174,14 @@ class MainActivity : ComponentActivity() {

companion object {
val tracks = listOf(
DefaultAudioItem(
"https://rntp.dev/example/Longing.mp3",
MediaType.DEFAULT,
title = "Longing (wide album art)",
artwork = "https://i.ytimg.com/vi/qod7eBE0mTk/maxresdefault.jpg",
artist = "David Chavez",
duration = 143 * 1000,
),
DefaultAudioItem(
"https://rntp.dev/example/Longing.mp3",
MediaType.DEFAULT,
Expand Down
3 changes: 3 additions & 0 deletions kotlin-audio/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ dependencies {
// The instrumentation test companion libraries
androidTestImplementation("de.mannodermaus.junit5:android-test-core:1.3.0")
androidTestRuntimeOnly("de.mannodermaus.junit5:android-test-runner:1.3.0")

// album art transformation
implementation 'jp.wasabeef.transformers:coil:1.0.6'
}

afterEvaluate {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ data class PlayerConfig(
* The audio content type.
*/
val audioContentType: AudioContentType = AudioContentType.MUSIC,

/**
* Whether to crop the album art to a square.
*/
val squareCropAlbumArt: Boolean = false,
/**
* The audio usage.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import com.google.android.exoplayer2.ui.PlayerNotificationManager.CustomActionRe
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import jp.wasabeef.transformers.coil.CropSquareTransformation
import okhttp3.Headers
import okhttp3.Headers.Companion.toHeaders

Expand All @@ -47,7 +48,8 @@ class NotificationManager internal constructor(
private val mediaSession: MediaSessionCompat,
private val mediaSessionConnector: MediaSessionConnector,
val event: NotificationEventHolder,
val playerEventHolder: PlayerEventHolder
val playerEventHolder: PlayerEventHolder,
private val squareCropAlbumArt: Boolean,
) : PlayerNotificationManager.NotificationListener {
private var pendingIntent: PendingIntent? = null
private val descriptionAdapter = object : PlayerNotificationManager.MediaDescriptionAdapter {
Expand Down Expand Up @@ -79,11 +81,14 @@ class NotificationManager internal constructor(
val headers = getNetworkHeaders()
val holder = player.currentMediaItem?.getAudioItemHolder()
if (artwork != null && holder?.artworkBitmap == null) {
context.imageLoader.enqueue(
ImageRequest.Builder(context)
var imageRequest = ImageRequest.Builder(context)
.data(artwork)
.headers(headers)
.target { result ->
if (squareCropAlbumArt) {
imageRequest = imageRequest.transformations(CropSquareTransformation())
}
context.imageLoader.enqueue(
imageRequest.target { result ->
val resultBitmap = (result as BitmapDrawable).bitmap
holder?.artworkBitmap = resultBitmap
invalidate()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,8 @@ abstract class BaseAudioPlayer internal constructor(
mediaSession,
mediaSessionConnector,
notificationEventHolder,
playerEventHolder
playerEventHolder,
playerConfig.squareCropAlbumArt
)

exoPlayer.addListener(PlayerListener())
Expand Down