feat: shuffle the whole library and add a sleep timer
CI / build (push) Failing after 20m30s

Library screen: an "All music" card with Shuffle all, which builds a local
queue from every cached song so playback does not need a mood session.

Player bar: a sleep timer with 15/30/45/60/90 minute presets. The shared store
shows the countdown and pauses playback; on Android the pause is also scheduled
inside the media service through a custom session command, so it still fires
when the app is backgrounded or the task is swiped away.

Playback errors from the platform player now surface in the UI instead of
failing silently (for example when no audio device is available).

Tests cover shuffle-all, the empty library, timer expiry and cancelling it.
This commit is contained in:
TapTap committed 2026-11-15 08:09:02 +00:00
1 parent f8953fb200
commit c7c1d59b70
10 files changed
+478 -42

No files matched your search

@@ -3,6 +3,7 @@ package win.taptap.musicmanager.client
import android.content.ComponentName
import android.content.Context
import android.net.Uri
import android.os.Bundle
import androidx.core.content.ContextCompat
import androidx.media3.common.MediaItem
import androidx.media3.common.MediaMetadata
@@ -108,6 +109,12 @@ class MediaControllerAudioPlayer(context: Context) : AudioPlayer {
withController { it.seekTo((seconds * 1000).toLong().coerceAtLeast(0L)) }
}
override fun scheduleSleepTimer(seconds: Long?): Boolean {
val current = controller ?: return false
val args = Bundle().apply { putLong(SLEEP_TIMER_SECONDS, seconds ?: 0L) }
return runCatching { current.sendCustomCommand(SLEEP_TIMER_COMMAND, args) }.isSuccess
}
override fun release() {
controller?.release()
MediaController.releaseFuture(future)
@@ -2,11 +2,23 @@ package win.taptap.musicmanager.client
import android.app.PendingIntent
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import androidx.media3.common.AudioAttributes
import androidx.media3.common.C
import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.session.MediaSession
import androidx.media3.session.MediaSessionService
import androidx.media3.session.SessionCommand
import androidx.media3.session.SessionResult
import com.google.common.util.concurrent.Futures
import com.google.common.util.concurrent.ListenableFuture
/** Custom session command used to arm or clear the sleep timer. */
internal const val SLEEP_TIMER_ACTION = "win.taptap.musicmanager.client.SLEEP_TIMER"
internal const val SLEEP_TIMER_SECONDS = "seconds"
internal val SLEEP_TIMER_COMMAND = SessionCommand(SLEEP_TIMER_ACTION, Bundle.EMPTY)
/**
* Hosts playback in a foreground service so audio continues when the activity
@@ -18,6 +30,8 @@ import androidx.media3.session.MediaSessionService
class PlaybackService : MediaSessionService() {
private var mediaSession: MediaSession? = null
private val handler = Handler(Looper.getMainLooper())
private val sleepTimer = Runnable { mediaSession?.player?.pause() }
override fun onCreate() {
super.onCreate()
@@ -45,6 +59,37 @@ class PlaybackService : MediaSessionService() {
mediaSession = MediaSession.Builder(this, player)
.setSessionActivity(sessionActivity)
.setCallback(object : MediaSession.Callback {
override fun onConnect(
session: MediaSession,
controller: MediaSession.ControllerInfo,
): MediaSession.ConnectionResult {
val commands = MediaSession.ConnectionResult.DEFAULT_SESSION_AND_LIBRARY_COMMANDS
.buildUpon()
.add(SLEEP_TIMER_COMMAND)
.build()
return MediaSession.ConnectionResult.AcceptedResultBuilder(session)
.setAvailableSessionCommands(commands)
.build()
}
override fun onCustomCommand(
session: MediaSession,
controller: MediaSession.ControllerInfo,
customCommand: SessionCommand,
args: Bundle,
): ListenableFuture<SessionResult> {
if (customCommand.customAction != SLEEP_TIMER_ACTION) {
return super.onCustomCommand(session, controller, customCommand, args)
}
handler.removeCallbacks(sleepTimer)
val seconds = args.getLong(SLEEP_TIMER_SECONDS, 0L)
if (seconds > 0) {
handler.postDelayed(sleepTimer, seconds * 1_000)
}
return Futures.immediateFuture(SessionResult(SessionResult.RESULT_SUCCESS))
}
})
.build()
}
@@ -59,6 +104,7 @@ class PlaybackService : MediaSessionService() {
}
override fun onDestroy() {
handler.removeCallbacks(sleepTimer)
mediaSession?.run {
player.release()
release()