Repository navigation
feat:gives persistent music playback/continue playing - #316
Merged
Merged
Conversation
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR resolves an issue where the Continue Playing song, playback position, and upcoming queue were reset/lost after pausing playback, minimizing LastWave, and swiping the app away from Recents (or when Android reclaimed the background process).
When playback was paused and the user swiped LastWave away from Recents:
1.
MusicPlaybackService.onTaskRemoved() called musicPlayer.stopAndClear(clearSession = false).
2.
Inside stopAndClear(), player.clearMediaItems() caused ExoPlayer to dispatch an asynchronous onEvents callback on the Main thread.
3.
ExoPlayer's listener called refresh(player), which observed an empty timeline, set _state.value to an empty MusicPlayerState(), and called persistPlaybackSession().
4.
persistPlaybackSession() checked sourceQueue.isEmpty(), evaluated to true, and called clearPersistedPlaybackSession(), erasing PLAYBACK_SESSION_KEY from lastwave_playback_session SharedPreferences.
5.
Upon relaunch, restorePlaybackSession() found no saved session in SharedPreferences, causing the Continue Playing section and upcoming queue to start completely empty.
What Was Changed & How It Works-
•
Non-Destructive Empty Queue Handling: Updated persistPlaybackSession() in MusicPlayer.kt to return early (if (sourceQueue.isEmpty()) return) when the in-memory queue is empty, rather than deleting the persisted session from disk.
•
Preserved Intentional Session Wiping: Session deletion on disk remains an explicit operation that occurs only when the user intentionally stops or clears playback (stopAndClear(clearSession = true)), closes the player, or when corrupt session JSON is encountered. Teardown transitions and empty ExoPlayer timelines no longer wipe disk storage.
•
Immediate Pause Persistence: Added a persistPlaybackSession() call inside MusicPlayer.pause() so the exact active track, playback position (positionMs), queue order, current index, shuffle, and repeat modes are saved to disk on Dispatchers.IO the instant playback is paused.
•
Non-Blocking Eager State Restoration: Upon relaunch, restorePlaybackSession() reads the saved session from SharedPreferences, restores _state.value immediately for UI rendering (Continue Playing / Mini Player), and lazily hydrates ExoPlayer in paused mode without blocking app startup.
Verification & Testing
•
Unit Tests: Executed .\gradlew.bat :app:testDebugUnitTest — All 350 unit tests passed.
•
Compilation: Executed .\gradlew.bat :app:compileDebugKotlin — BUILD SUCCESSFUL.
•
APK Build: Executed .\gradlew.bat assembleDebug — BUILD SUCCESSFUL.