-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add PlayerListener & Ui changes [Not tested]
- Loading branch information
1 parent
6982ec8
commit 474b259
Showing
6 changed files
with
244 additions
and
64 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
app/src/main/java/dev/brahmkshatriya/echo/ui/player/PlayerHelper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package dev.brahmkshatriya.echo.ui.player | ||
|
||
import android.annotation.SuppressLint | ||
import android.graphics.Bitmap | ||
import androidx.core.net.toUri | ||
import androidx.media3.common.MediaMetadata | ||
import androidx.media3.common.util.UnstableApi | ||
import dev.brahmkshatriya.echo.data.models.ImageHolder | ||
import dev.brahmkshatriya.echo.data.models.Track | ||
import java.nio.ByteBuffer | ||
|
||
|
||
interface PlayerHelper { | ||
companion object { | ||
|
||
@SuppressLint("UnsafeOptInUsageError") | ||
fun Track.toMetaData() = MediaMetadata.Builder() | ||
.setTitle(title) | ||
.setArtist(artists.firstOrNull()?.name) | ||
.setArtwork(cover) | ||
.build() | ||
|
||
@UnstableApi | ||
private fun MediaMetadata.Builder.setArtwork(cover: ImageHolder?): MediaMetadata.Builder { | ||
cover?.let { | ||
return when (it) { | ||
is ImageHolder.UrlHolder -> setArtworkUri(it.url.toUri()) | ||
is ImageHolder.UriHolder -> setArtworkUri(it.uri) | ||
is ImageHolder.BitmapHolder -> setArtworkData( | ||
it.bitmap.toByteArray(), | ||
MediaMetadata.PICTURE_TYPE_FILE_ICON | ||
) | ||
} | ||
} | ||
return this | ||
} | ||
|
||
private fun Bitmap.toByteArray(): ByteArray { | ||
val size: Int = rowBytes * height | ||
val byteBuffer = ByteBuffer.allocate(size) | ||
copyPixelsToBuffer(byteBuffer) | ||
return byteBuffer.array() | ||
} | ||
|
||
|
||
fun Long.toTimeString(): String { | ||
val seconds = this / 1000 | ||
val minutes = seconds / 60 | ||
val hours = minutes / 60 | ||
return if (hours > 0) { | ||
String.format("%02d:%02d:%02d", hours, minutes % 60, seconds % 60) | ||
} else { | ||
String.format("%02d:%02d", minutes, seconds % 60) | ||
} | ||
} | ||
} | ||
} | ||
|
115 changes: 115 additions & 0 deletions
115
app/src/main/java/dev/brahmkshatriya/echo/ui/player/PlayerListener.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package dev.brahmkshatriya.echo.ui.player | ||
|
||
import android.annotation.SuppressLint | ||
import android.os.Handler | ||
import android.os.Looper | ||
import androidx.media3.common.C | ||
import androidx.media3.common.MediaMetadata | ||
import androidx.media3.common.Player | ||
import androidx.media3.session.MediaController | ||
import dev.brahmkshatriya.echo.data.models.Track | ||
import dev.brahmkshatriya.echo.databinding.BottomPlayerBinding | ||
import dev.brahmkshatriya.echo.ui.player.PlayerHelper.Companion.toTimeString | ||
import dev.brahmkshatriya.echo.ui.utils.loadInto | ||
|
||
class PlayerListener(val player: MediaController, val binding: BottomPlayerBinding) : | ||
Player.Listener { | ||
init { | ||
//Poll each second to update the seekbar | ||
val handler = Handler(Looper.getMainLooper()) | ||
val runnable = object : Runnable { | ||
override fun run() { | ||
binding.collapsedSeekBar.progress = player.currentPosition.toInt() | ||
binding.expandedSeekBar.value = player.currentPosition.toFloat() | ||
|
||
binding.trackCurrentTime.text = player.currentPosition.toTimeString() | ||
|
||
handler.postDelayed(this, 1000) | ||
} | ||
} | ||
handler.post(runnable) | ||
} | ||
@SuppressLint("SwitchIntDef") | ||
override fun onPlaybackStateChanged(playbackState: Int) { | ||
when (playbackState) { | ||
Player.STATE_BUFFERING -> { | ||
binding.trackPlayPause.isEnabled = false | ||
binding.collapsedTrackPlayPause.isEnabled = false | ||
} | ||
|
||
Player.STATE_READY -> { | ||
binding.trackPlayPause.isEnabled = true | ||
binding.collapsedTrackPlayPause.isEnabled = true | ||
} | ||
} | ||
} | ||
|
||
override fun onIsPlayingChanged(isPlaying: Boolean) { | ||
binding.trackPlayPause.isEnabled = false | ||
binding.collapsedTrackPlayPause.isEnabled = false | ||
|
||
binding.trackPlayPause.isChecked = isPlaying | ||
binding.collapsedTrackPlayPause.isChecked = isPlaying | ||
|
||
binding.trackPlayPause.isEnabled = true | ||
binding.collapsedTrackPlayPause.isEnabled = true | ||
} | ||
|
||
|
||
private val tracks = mutableMapOf<MediaMetadata, Track>() | ||
fun map(metadata: MediaMetadata, track: Track) { | ||
tracks[metadata] = track | ||
} | ||
|
||
override fun onMediaMetadataChanged(mediaMetadata: MediaMetadata) { | ||
|
||
val track = tracks[mediaMetadata] ?: return | ||
binding.collapsedTrackTitle.text = track.title | ||
binding.expandedTrackTitle.text = track.title | ||
|
||
track.artists.joinToString(" ") { it.name }.run { | ||
binding.collapsedTrackAuthor.text = this | ||
binding.expandedTrackAuthor.text = this | ||
} | ||
track.cover?.run { | ||
loadInto(binding.collapsedTrackCover) | ||
loadInto(binding.expandedTrackCover) | ||
} | ||
|
||
binding.collapsedSeekBar.isIndeterminate = true | ||
binding.expandedSeekBar.isEnabled = false | ||
|
||
binding.trackTotalTime.text = null | ||
} | ||
|
||
override fun onPositionDiscontinuity( | ||
oldPosition: Player.PositionInfo, | ||
newPosition: Player.PositionInfo, | ||
reason: Int | ||
) { | ||
if (reason != Player.DISCONTINUITY_REASON_SEEK && reason != Player.DISCONTINUITY_REASON_SEEK_ADJUSTMENT) { | ||
binding.collapsedSeekBar.progress = newPosition.positionMs.toInt() | ||
binding.expandedSeekBar.value = newPosition.positionMs.toFloat() | ||
|
||
binding.trackCurrentTime.text = newPosition.positionMs.toTimeString() | ||
} | ||
} | ||
|
||
override fun onPlayWhenReadyChanged(playWhenReady: Boolean, reason: Int) { | ||
if (!playWhenReady) return | ||
if(player.duration == C.TIME_UNSET) return | ||
|
||
binding.collapsedSeekBar.isIndeterminate = false | ||
binding.expandedSeekBar.isEnabled = true | ||
|
||
binding.collapsedSeekBar.max = player.duration.toInt() | ||
binding.expandedSeekBar.valueTo = player.duration.toFloat() | ||
|
||
binding.trackTotalTime.text = player.duration.toTimeString() | ||
} | ||
|
||
override fun onPlaylistMetadataChanged(mediaMetadata: MediaMetadata) { | ||
super.onPlaylistMetadataChanged(mediaMetadata) | ||
println(mediaMetadata) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<selector xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<item android:state_checked="true" android:color="?colorOnPrimary" /> | ||
<item android:state_checked="true" android:color="?colorOnSurfaceVariant" /> | ||
<item android:color="?attr/colorPrimary" /> | ||
</selector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<selector xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<item android:state_enabled="false" android:color="?color" /> | ||
<item android:color="?attr/colorPrimary" /> | ||
</selector> |
Oops, something went wrong.