Skip to content

Commit

Permalink
chore: improve metadata handling (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
puckey authored Jul 18, 2023
1 parent 5b82ce3 commit 4caf58f
Show file tree
Hide file tree
Showing 7 changed files with 406 additions and 287 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,4 @@ data class PlayerConfig(
* The audio content type.
*/
val audioContentType: AudioContentType = AudioContentType.MUSIC,

/**
* Throttle interval for updating notification UI
*
* Android applies a rate limit when updating a notification (Default is 5 times per second)
*
* This flag will reduce the update rate when user interact with the notification consecutively
*/
val androidNotificationThrottleInterval: Long = BaseAudioPlayer.ANDROID_NOTIFICATION_UPDATE_THROTTLE_INTERVAL
)

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.os.Bundle
import android.os.ResultReceiver
import android.support.v4.media.RatingCompat
import android.support.v4.media.session.MediaSessionCompat
import android.support.v4.media.MediaMetadataCompat
import androidx.annotation.CallSuper
import androidx.core.content.ContextCompat
import androidx.media.AudioAttributesCompat
Expand Down Expand Up @@ -38,6 +39,7 @@ import com.doublesymmetry.kotlinaudio.models.PlayerOptions
import com.doublesymmetry.kotlinaudio.models.PositionChangedReason
import com.doublesymmetry.kotlinaudio.notification.NotificationManager
import com.doublesymmetry.kotlinaudio.players.components.PlayerCache
import com.doublesymmetry.kotlinaudio.players.components.getAudioItemHolder
import com.doublesymmetry.kotlinaudio.utils.isUriLocal
import com.google.android.exoplayer2.C
import com.google.android.exoplayer2.DefaultLoadControl
Expand Down Expand Up @@ -95,7 +97,7 @@ abstract class BaseAudioPlayer internal constructor(
open val playerOptions: PlayerOptions = DefaultPlayerOptions()

open val currentItem: AudioItem?
get() = exoPlayer.currentMediaItem?.localConfiguration?.tag as AudioItem?
get() = exoPlayer.currentMediaItem?.getAudioItemHolder()?.audioItem

var playbackError: PlaybackError? = null
var playerState: AudioPlayerState = AudioPlayerState.IDLE
Expand Down Expand Up @@ -233,8 +235,7 @@ abstract class BaseAudioPlayer internal constructor(
mediaSession,
mediaSessionConnector,
notificationEventHolder,
playerEventHolder,
playerConfig.androidNotificationThrottleInterval
playerEventHolder
)

exoPlayer.addListener(PlayerListener())
Expand All @@ -256,6 +257,9 @@ abstract class BaseAudioPlayer internal constructor(
.build();
exoPlayer.setAudioAttributes(audioAttributes, playerConfig.handleAudioFocus);
mediaSessionConnector.setPlayer(playerToUse)
mediaSessionConnector.setMediaMetadataProvider {
notificationManager.getMediaMetadataCompat()
}
}

playerEventHolder.updateAudioPlayerState(AudioPlayerState.IDLE)
Expand Down Expand Up @@ -313,13 +317,9 @@ abstract class BaseAudioPlayer internal constructor(
}
}

internal fun updateNotificationMetadataIfAutomatic() {
internal fun resetNotificationMetadataIfAutomatic() {
if (automaticallyUpdateNotificationMetadata) {
notificationManager.notificationMetadata = NotificationMetadata(
currentItem?.title,
currentItem?.artist,
currentItem?.artwork
)
notificationManager.notificationMetadata = null
}
}

Expand Down Expand Up @@ -440,14 +440,12 @@ abstract class BaseAudioPlayer internal constructor(
exoPlayer.seekTo(positionMs)
}

private fun getMediaItemFromAudioItem(audioItem: AudioItem): MediaItem {
return MediaItem.Builder().setUri(audioItem.audioUrl).setTag(AudioItemHolder(audioItem)).build()
}

protected fun getMediaSourceFromAudioItem(audioItem: AudioItem): MediaSource {
val factory: DataSource.Factory
val uri = Uri.parse(audioItem.audioUrl)
val mediaItem = getMediaItemFromAudioItem(audioItem)
val mediaItem = MediaItem.Builder()
.setUri(audioItem.audioUrl)
.setTag(AudioItemHolder(audioItem))
.build()

val userAgent =
if (audioItem.options == null || audioItem.options!!.userAgent.isNullOrBlank()) {
Expand All @@ -456,7 +454,7 @@ abstract class BaseAudioPlayer internal constructor(
audioItem.options!!.userAgent
}

factory = when {
val factory: DataSource.Factory = when {
audioItem.options?.resourceId != null -> {
val raw = RawResourceDataSource(context)
raw.open(DataSpec(uri))
Expand Down Expand Up @@ -676,7 +674,7 @@ abstract class BaseAudioPlayer internal constructor(
)
}

updateNotificationMetadataIfAutomatic()
resetNotificationMetadataIfAutomatic()
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.doublesymmetry.kotlinaudio.players

import android.content.Context
import com.doublesymmetry.kotlinaudio.models.*
import com.doublesymmetry.kotlinaudio.players.components.getAudioItemHolder
import com.google.android.exoplayer2.C
import com.google.android.exoplayer2.IllegalSeekPositionException
import com.google.android.exoplayer2.source.MediaSource
Expand All @@ -22,7 +23,7 @@ class QueuedAudioPlayer(
get() = exoPlayer.currentMediaItemIndex

override val currentItem: AudioItem?
get() = (queue.getOrNull(currentIndex)?.mediaItem?.localConfiguration?.tag as AudioItemHolder?)?.audioItem
get() = queue.getOrNull(currentIndex)?.mediaItem?.getAudioItemHolder()?.audioItem

val nextIndex: Int?
get() {
Expand All @@ -37,22 +38,22 @@ class QueuedAudioPlayer(
}

val items: List<AudioItem>
get() = queue.map { (it.mediaItem.localConfiguration?.tag as AudioItemHolder).audioItem }
get() = queue.map { it.mediaItem.getAudioItemHolder().audioItem }

val previousItems: List<AudioItem>
get() {
return if (queue.isEmpty()) emptyList()
else queue
.subList(0, exoPlayer.currentMediaItemIndex)
.map { (it.mediaItem.localConfiguration?.tag as AudioItemHolder).audioItem }
.map { it.mediaItem.getAudioItemHolder().audioItem }
}

val nextItems: List<AudioItem>
get() {
return if (queue.isEmpty()) emptyList()
else queue
.subList(exoPlayer.currentMediaItemIndex, queue.lastIndex)
.map { (it.mediaItem.localConfiguration?.tag as AudioItemHolder).audioItem }
.map { it.mediaItem.getAudioItemHolder().audioItem }
}

val nextItem: AudioItem?
Expand Down Expand Up @@ -217,7 +218,7 @@ class QueuedAudioPlayer(
val mediaSource = getMediaSourceFromAudioItem(item)
queue[index] = mediaSource
if (index == currentIndex) {
updateNotificationMetadataIfAutomatic()
resetNotificationMetadataIfAutomatic()
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.doublesymmetry.kotlinaudio.players.components

import com.doublesymmetry.kotlinaudio.models.AudioItemHolder
import com.google.android.exoplayer2.MediaItem

fun MediaItem.getAudioItemHolder(): AudioItemHolder {
return localConfiguration!!.tag as AudioItemHolder
}

This file was deleted.

This file was deleted.

0 comments on commit 4caf58f

Please sign in to comment.