From 7e2afdb287232b6836d15f4f72634408b1a1ee50 Mon Sep 17 00:00:00 2001 From: brahmkshatriya <69040506+brahmkshatriya@users.noreply.github.com> Date: Mon, 12 Feb 2024 19:44:47 +0530 Subject: [PATCH] Allow tracks to continue when is removed from recents --- .../dev/brahmkshatriya/echo/PlaybackService.kt | 2 +- .../dev/brahmkshatriya/echo/ui/player/Player.kt | 3 +++ .../echo/ui/player/PlayerListener.kt | 16 +++++++++++++--- .../echo/ui/player/PlayerViewModel.kt | 1 - 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/dev/brahmkshatriya/echo/PlaybackService.kt b/app/src/main/java/dev/brahmkshatriya/echo/PlaybackService.kt index d6d83780..b9060259 100644 --- a/app/src/main/java/dev/brahmkshatriya/echo/PlaybackService.kt +++ b/app/src/main/java/dev/brahmkshatriya/echo/PlaybackService.kt @@ -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) diff --git a/app/src/main/java/dev/brahmkshatriya/echo/ui/player/Player.kt b/app/src/main/java/dev/brahmkshatriya/echo/ui/player/Player.kt index 45fc9816..c1952cc9 100644 --- a/app/src/main/java/dev/brahmkshatriya/echo/ui/player/Player.kt +++ b/app/src/main/java/dev/brahmkshatriya/echo/ui/player/Player.kt @@ -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) { diff --git a/app/src/main/java/dev/brahmkshatriya/echo/ui/player/PlayerListener.kt b/app/src/main/java/dev/brahmkshatriya/echo/ui/player/PlayerListener.kt index b9031ea9..947cef62 100644 --- a/app/src/main/java/dev/brahmkshatriya/echo/ui/player/PlayerListener.kt +++ b/app/src/main/java/dev/brahmkshatriya/echo/ui/player/PlayerListener.kt @@ -40,12 +40,14 @@ class PlayerListener( viewModel.isPlaying.value = isPlaying } + companion object { + val tracks = mutableMapOf() + } - private val tracks = mutableMapOf() 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 } @@ -77,7 +79,6 @@ class PlayerListener( } handler.postDelayed(updateProgressRunnable, delayMs) } - } private fun updateNavigation() { @@ -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() + } } diff --git a/app/src/main/java/dev/brahmkshatriya/echo/ui/player/PlayerViewModel.kt b/app/src/main/java/dev/brahmkshatriya/echo/ui/player/PlayerViewModel.kt index ccce86ae..07eca45a 100644 --- a/app/src/main/java/dev/brahmkshatriya/echo/ui/player/PlayerViewModel.kt +++ b/app/src/main/java/dev/brahmkshatriya/echo/ui/player/PlayerViewModel.kt @@ -34,7 +34,6 @@ class PlayerViewModel @Inject constructor( private suspend fun loadStreamable(track: Track) = TrackWithStream(track, trackClient.getStreamable(track)) - private val queue = mutableListOf() private suspend fun loadAndAddToQueue(track: Track): Int { val stream = loadStreamable(track)