Skip to content

Commit

Permalink
Allow tracks to continue when is removed from recents
Browse files Browse the repository at this point in the history
  • Loading branch information
brahmkshatriya committed Feb 12, 2024
1 parent ccbb8c1 commit 7e2afdb
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class PlaybackService : MediaSessionService() {
.putExtra("fromNotification", true)

val pendingIntent = PendingIntent
.getActivity(this, 0, intent,PendingIntent.FLAG_IMMUTABLE)
.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE)

mediaSession = MediaSession.Builder(this, player)
.setSessionActivity(pendingIntent)
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/dev/brahmkshatriya/echo/ui/player/Player.kt
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ class Player(

val listener = uiViewModel.getListener(player)
player.addListener(listener)
player.currentMediaItem?.let {
listener.update(it.mediaId)
}

activity.apply {
observe(playerViewModel.playPause) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ class PlayerListener(
viewModel.isPlaying.value = isPlaying
}

companion object {
val tracks = mutableMapOf<String, Track>()
}

private val tracks = mutableMapOf<String,Track>()
override fun onMediaItemTransition(mediaItem: MediaItem?, reason: Int) {
val id = mediaItem?.mediaId ?: return
val track = mediaItem.localConfiguration?.tag as Track?
if(track != null) tracks[id] = track
if (track != null) tracks[id] = track
viewModel.track.value = tracks[id] ?: return
}

Expand Down Expand Up @@ -77,7 +79,6 @@ class PlayerListener(
}
handler.postDelayed(updateProgressRunnable, delayMs)
}

}

private fun updateNavigation() {
Expand All @@ -87,4 +88,13 @@ class PlayerListener(
viewModel.nextEnabled.value = enableNext
viewModel.previousEnabled.value = enablePrevious
}

fun update(mediaId: String){
viewModel.track.value = tracks[mediaId]
viewModel.totalDuration.value = player.duration.toInt()
viewModel.isPlaying.value = player.isPlaying
viewModel.buffering.value = player.playbackState == Player.STATE_BUFFERING
updateNavigation()
updateProgress()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class PlayerViewModel @Inject constructor(
private suspend fun loadStreamable(track: Track) =
TrackWithStream(track, trackClient.getStreamable(track))


private val queue = mutableListOf<TrackWithStream>()
private suspend fun loadAndAddToQueue(track: Track): Int {
val stream = loadStreamable(track)
Expand Down

0 comments on commit 7e2afdb

Please sign in to comment.