fix: stop playback if app is stopped
This commit is contained in:
@@ -62,6 +62,7 @@ class PlaybackService : Service() {
|
||||
|
||||
private var queue: List<TrackQueueItem> = emptyList()
|
||||
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. */
|
||||
@Volatile
|
||||
@@ -524,15 +525,37 @@ class PlaybackService : Service() {
|
||||
.build()
|
||||
}
|
||||
|
||||
override fun onTaskRemoved(rootIntent: Intent?) {
|
||||
stopPlaybackAndTeardown()
|
||||
stopSelf()
|
||||
super.onTaskRemoved(rootIntent)
|
||||
}
|
||||
|
||||
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 = null
|
||||
positionCachePollJob?.cancel()
|
||||
positionCachePollJob = null
|
||||
releaseEngine()
|
||||
queue = emptyList()
|
||||
index = 0
|
||||
cachedPlaybackPositionMs = 0L
|
||||
_uiState.value = PlaybackUiState.initial()
|
||||
if (::mediaSession.isInitialized) {
|
||||
mediaSession.run {
|
||||
isActive = false
|
||||
release()
|
||||
}
|
||||
super.onDestroy()
|
||||
}
|
||||
stopForeground(STOP_FOREGROUND_REMOVE)
|
||||
}
|
||||
|
||||
data class PlaybackUiState(
|
||||
|
||||
Reference in New Issue
Block a user