fix: release MediaSession immediately on playback stop to clear system control card

This commit is contained in:
tunbmoon
2026-03-08 19:42:44 +08:00
parent 14108fe6ae
commit ffaa6f7563
2 changed files with 41 additions and 11 deletions
@@ -61,8 +61,29 @@ class PlaybackService : MediaSessionService() {
startForeground(NOTIFICATION_ID, createNotification())
} else {
stopForeground(STOP_FOREGROUND_DETACH)
mediaSession?.let {
it.release()
mediaSession = null
val notificationManager = getSystemService(NotificationManager::class.java)
notificationManager?.cancel(NOTIFICATION_ID)
}
}
}
override fun onPlaybackStateChanged(playbackState: Int) {
when (playbackState) {
Player.STATE_ENDED, Player.STATE_IDLE -> {
if (!player.isPlaying) {
stopSelfAndCleanup()
}
}
}
}
override fun onPlayerError(error: androidx.media3.common.PlaybackException) {
stopSelfAndCleanup()
}
})
if (player.isPlaying) {
@@ -106,10 +127,27 @@ class PlaybackService : MediaSessionService() {
return mediaSession
}
override fun onDestroy() {
mediaSession?.release()
mediaSession = null
private fun stopSelfAndCleanup() {
mediaSession?.let {
it.release()
mediaSession = null
}
currentPlayer = null
val notificationManager = getSystemService(NotificationManager::class.java)
notificationManager?.cancel(NOTIFICATION_ID)
stopForeground(STOP_FOREGROUND_REMOVE)
stopSelf()
}
override fun onDestroy() {
stopSelfAndCleanup()
super.onDestroy()
}
override fun onTaskRemoved(rootIntent: Intent?) {
super.onTaskRemoved(rootIntent)
stopSelfAndCleanup()
}
}
@@ -2,15 +2,12 @@ package com.antoniegil.astronia.ui.component
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.PlayArrow
@@ -23,19 +20,14 @@ import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clipToBounds
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalView
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import com.antoniegil.astronia.R
import com.antoniegil.astronia.ui.common.HapticFeedback.slightHapticFeedback
import com.antoniegil.astronia.util.M3U8Channel
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat
import java.util.Date
@Composable
fun ChannelListSection(