Skip to content

Commit

Permalink
Fix duration in metadata adapter (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
dcvz authored Sep 22, 2023
1 parent bbdbf32 commit e52ee51
Showing 1 changed file with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import com.doublesymmetry.kotlinaudio.models.MediaSessionCallback
import com.doublesymmetry.kotlinaudio.models.NotificationState
import com.doublesymmetry.kotlinaudio.models.filterForNotification
import com.doublesymmetry.kotlinaudio.players.components.getAudioItemHolder
import com.google.android.exoplayer2.C
import com.google.android.exoplayer2.Player
import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector
import com.google.android.exoplayer2.ext.mediasession.TimelineQueueNavigator
Expand Down Expand Up @@ -194,7 +195,12 @@ class NotificationManager internal constructor(
private fun getDuration(index: Int? = null): Long {
val mediaItem = if (index == null) player.currentMediaItem
else player.getMediaItemAt(index)
return mediaItem?.getAudioItemHolder()?.audioItem?.duration ?: -1

return if (player.isCurrentMediaItemDynamic || player.duration == C.TIME_UNSET) {
mediaItem?.getAudioItemHolder()?.audioItem?.duration ?: -1
} else {
player.duration
}
}

private fun getUserRating(index: Int? = null): RatingCompat? {
Expand Down Expand Up @@ -353,12 +359,25 @@ class NotificationManager internal constructor(
}

public fun getMediaMetadataCompat(): MediaMetadataCompat {
val currentItemMetadata = player.currentMediaItem?.mediaMetadata

return MediaMetadataCompat.Builder().apply {
getArtist()?.let {
putString(MediaMetadataCompat.METADATA_KEY_ARTIST, it)
}
getTitle()?.let {
putString(MediaMetadataCompat.METADATA_KEY_TITLE, it)
putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_TITLE, it)
}
currentItemMetadata?.subtitle?.let {
putString(
MediaMetadataCompat.METADATA_KEY_DISPLAY_SUBTITLE, it.toString()
)
}
currentItemMetadata?.description?.let {
putString(
MediaMetadataCompat.METADATA_KEY_DISPLAY_DESCRIPTION, it.toString()
)
}
getAlbumTitle()?.let {
putString(MediaMetadataCompat.METADATA_KEY_ALBUM, it)
Expand Down

0 comments on commit e52ee51

Please sign in to comment.