Skip to content

Commit

Permalink
Notification icon added & seekbar changed
Browse files Browse the repository at this point in the history
  • Loading branch information
brahmkshatriya committed Feb 6, 2024
1 parent 748b59c commit 2a30f39
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 39 deletions.
5 changes: 5 additions & 0 deletions app/src/main/java/dev/brahmkshatriya/echo/PlaybackService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dev.brahmkshatriya.echo
import androidx.annotation.OptIn
import androidx.media3.common.util.UnstableApi
import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.session.DefaultMediaNotificationProvider
import androidx.media3.session.MediaSession
import androidx.media3.session.MediaSessionService

Expand All @@ -14,6 +15,10 @@ class PlaybackService : MediaSessionService() {
super.onCreate()
val player = ExoPlayer.Builder(this).build()
mediaSession = MediaSession.Builder(this, player).build()
setMediaNotificationProvider(
DefaultMediaNotificationProvider.Builder(this).build()
.also { it.setSmallIcon(R.drawable.ic_mono) }
)
}

override fun onDestroy() {
Expand Down
22 changes: 17 additions & 5 deletions app/src/main/java/dev/brahmkshatriya/echo/ui/player/Player.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dev.brahmkshatriya.echo.ui.player

import android.view.View
import android.widget.SeekBar
import android.widget.SeekBar.OnSeekBarChangeListener
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.media3.common.MediaItem
Expand All @@ -17,6 +19,7 @@ import dev.brahmkshatriya.echo.R
import dev.brahmkshatriya.echo.data.models.StreamableAudio
import dev.brahmkshatriya.echo.databinding.BottomPlayerBinding
import dev.brahmkshatriya.echo.ui.player.PlayerHelper.Companion.toMetaData
import dev.brahmkshatriya.echo.ui.player.PlayerHelper.Companion.toTimeString
import dev.brahmkshatriya.echo.ui.utils.dpToPx
import dev.brahmkshatriya.echo.ui.utils.updatePaddingWithSystemInsets
import kotlinx.coroutines.flow.collectLatest
Expand All @@ -29,7 +32,7 @@ class Player(
private val binding: BottomPlayerBinding
) {

val viewModel by activity.viewModels<PlayerViewModel>()
private val viewModel: PlayerViewModel by activity.viewModels()

init {
applyView()
Expand Down Expand Up @@ -93,11 +96,20 @@ class Player(
player.seekToPreviousMediaItem()
}

binding.expandedSeekBar.addOnChangeListener { _, value, fromUser ->
if (fromUser) {
player.seekTo(value.toLong())

binding.expandedSeekBar.setOnSeekBarChangeListener(object : OnSeekBarChangeListener {
override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {
binding.trackCurrentTime.text = p1.toLong().toTimeString()
}
}

override fun onStartTrackingTouch(p0: SeekBar?) {}
override fun onStopTrackingTouch(p0: SeekBar?) {
p0?.progress?.let {
player.seekTo(it.toLong())
binding.trackCurrentTime.text = it.toLong().toTimeString()
}
}
})

val listener = PlayerListener(player, binding, playPauseListener)
player.addListener(listener)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,31 @@ import dev.brahmkshatriya.echo.ui.player.PlayerHelper.Companion.toTimeString
import dev.brahmkshatriya.echo.ui.utils.loadInto

class PlayerListener(
val player: MediaController,
val binding: BottomPlayerBinding,
val playPauseListener: MaterialCheckBox.OnCheckedStateChangedListener
private val player: MediaController,
private val binding: BottomPlayerBinding,
private val playPauseListener: MaterialCheckBox.OnCheckedStateChangedListener
) :
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.collapsedSeekBar.secondaryProgress = player.bufferedPosition.toInt()

binding.trackCurrentTime.text = player.currentPosition.toTimeString()

if(!binding.expandedSeekBar.isPressed) {
binding.collapsedSeekBar.progress = player.currentPosition.toInt()
binding.collapsedSeekBar.secondaryProgress = player.bufferedPosition.toInt()

binding.expandedSeekBar.secondaryProgress = player.bufferedPosition.toInt()
binding.expandedSeekBar.progress = player.currentPosition.toInt()

binding.trackCurrentTime.text = player.currentPosition.toTimeString()
}
handler.postDelayed(this, 1000)
}
}
handler.post(runnable)
}

@SuppressLint("SwitchIntDef")
override fun onPlaybackStateChanged(playbackState: Int) {
when (playbackState) {
Expand All @@ -48,13 +51,13 @@ class PlayerListener(
binding.trackPlayPause.isEnabled = true
binding.collapsedTrackPlayPause.isEnabled = true

if(player.duration == C.TIME_UNSET) throw IllegalStateException("Duration is not set")
if (player.duration == C.TIME_UNSET) throw IllegalStateException("Duration is not set")

binding.collapsedSeekBar.isIndeterminate = false
binding.expandedSeekBar.isEnabled = true

binding.collapsedSeekBar.max = player.duration.toInt() + 100
binding.expandedSeekBar.valueTo = player.duration.toFloat() + 100
binding.collapsedSeekBar.max = player.duration.toInt()
binding.expandedSeekBar.max = player.duration.toInt()

binding.trackTotalTime.text = player.duration.toTimeString()
}
Expand Down Expand Up @@ -106,7 +109,7 @@ class PlayerListener(
) {
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.expandedSeekBar.progress = newPosition.positionMs.toInt()

binding.trackCurrentTime.text = newPosition.positionMs.toTimeString()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import com.bumptech.glide.Glide
import com.bumptech.glide.load.model.GlideUrl
import dev.brahmkshatriya.echo.data.models.ImageHolder

fun ImageHolder.loadInto(imageView: ImageView){
if(this is ImageHolder.BitmapHolder)
fun ImageHolder.loadInto(imageView: ImageView) {
if (this is ImageHolder.BitmapHolder)
Glide.with(imageView)
.load(this.bitmap)
.into(imageView)
if(this is ImageHolder.UrlHolder)
if (this is ImageHolder.UrlHolder)
Glide.with(imageView)
.load(GlideUrl(this.url){ this.headers })
.load(GlideUrl(this.url) { this.headers })
.into(imageView)
if (this is ImageHolder.UriHolder)
Glide.with(imageView)
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_thumb.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FF000000"
android:pathData="M12,12m-8,0a8,8 0,1 1,16 0a8,8 0,1 1,-16 0"/>
</vector>
30 changes: 14 additions & 16 deletions app/src/main/res/layout/bottom_player.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,19 @@
tools:text="00:00" />
</FrameLayout>

<com.google.android.material.slider.Slider
<SeekBar
android:id="@+id/expandedSeekBar"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginStart="-2dp"
android:layout_marginEnd="-2dp"
app:labelBehavior="gone"
app:thumbColor="?colorTertiary"
app:thumbHeight="16dp"
app:thumbTrackGapSize="12dp"
app:thumbWidth="16dp"
app:trackColorActive="?colorTertiary"
app:trackColorInactive="?colorTertiaryContainer"
app:trackHeight="4dp" />
android:progressTint="?colorTertiary"
android:secondaryProgressTint="?colorTertiary"
android:progressBackgroundTint="?attr/colorOutlineVariant"
android:progress="10"
android:secondaryProgress="20"
android:thumb="@drawable/ic_thumb"
android:thumbTint="?colorTertiary" />

<LinearLayout
android:layout_width="match_parent"
Expand Down Expand Up @@ -178,16 +177,15 @@
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.google.android.material.progressindicator.LinearProgressIndicator
<ProgressBar
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:id="@+id/collapsedSeekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_height="2dp"
android:layout_gravity="bottom"
android:layout_marginStart="4dp"
android:layout_marginEnd="4dp"
app:indicatorColor="?colorTertiary"
app:trackColor="?colorTertiaryContainer"
app:trackThickness="2dp" />
android:progressTint="?colorTertiary"
android:secondaryProgressTint="?colorTertiary"
android:progressBackgroundTint="?attr/colorOutlineVariant" />

<LinearLayout
android:layout_width="match_parent"
Expand Down

0 comments on commit 2a30f39

Please sign in to comment.