fix: stop playback if app is stopped

This commit is contained in:
2026-05-30 20:21:50 +02:00
parent 9d161bd2a9
commit 306ad83d12

View File

@@ -62,6 +62,7 @@ class PlaybackService : Service() {
private var queue: List<TrackQueueItem> = emptyList() private var queue: List<TrackQueueItem> = emptyList()
private var index: Int = 0 private var index: Int = 0
private var tornDown = false
/** Updated on the main thread whenever progress is read from the engine — safe for sensor threads. */ /** Updated on the main thread whenever progress is read from the engine — safe for sensor threads. */
@Volatile @Volatile
@@ -524,15 +525,37 @@ class PlaybackService : Service() {
.build() .build()
} }
override fun onTaskRemoved(rootIntent: Intent?) {
stopPlaybackAndTeardown()
stopSelf()
super.onTaskRemoved(rootIntent)
}
override fun onDestroy() { override fun onDestroy() {
stopPlaybackAndTeardown()
super.onDestroy()
}
/** Stops audio, clears queue state, and removes the foreground notification. Idempotent. */
private fun stopPlaybackAndTeardown() {
if (tornDown) return
tornDown = true
positionPollJob?.cancel() positionPollJob?.cancel()
positionPollJob = null
positionCachePollJob?.cancel() positionCachePollJob?.cancel()
positionCachePollJob = null
releaseEngine() releaseEngine()
queue = emptyList()
index = 0
cachedPlaybackPositionMs = 0L
_uiState.value = PlaybackUiState.initial()
if (::mediaSession.isInitialized) {
mediaSession.run { mediaSession.run {
isActive = false isActive = false
release() release()
} }
super.onDestroy() }
stopForeground(STOP_FOREGROUND_REMOVE)
} }
data class PlaybackUiState( data class PlaybackUiState(