Harden the playback service and make next/previous work outside the app

- Resolve every item from a controller against the station list: an item
  without a URI (voice requests from Assistant or Android Auto) no longer
  crashes the player, and other apps can no longer start arbitrary streams.
- Offer next/previous to the notification, headset buttons and Android Auto
  through a ForwardingSimpleBasePlayer that switches the station; the player
  still holds only the station on air, so no other stream is prepared.
- Do not spend reconnect attempts while offline; reconnect when the network
  comes back instead of pausing for good after about four minutes.
- Swiping the app away during an error or a pending reconnect stops the
  service; a short rebuffer while playing does not.
- Loudness Enhancer applies a 6 dB boost by default; the old default of 0
  made the switch do nothing. The unused gain setter chain is removed.
- Count station switches made outside the app in recents and play counts.
This commit is contained in:
2026-11-12 16:43:34 +02:00
parent ac19416c00
commit 8cd9133803
10 changed files with 181 additions and 112 deletions
@@ -0,0 +1,45 @@
package cc.skysparkle.fogwave.player
import androidx.annotation.OptIn
import androidx.media3.common.PlaybackException
import androidx.media3.common.Player
import androidx.media3.common.util.UnstableApi
import androidx.media3.datasource.HttpDataSource
// Kept apart from the service and the UI controller so the decisions can be unit tested.
internal const val MAX_RETRY_ATTEMPTS = 12
internal const val MAX_PERMANENT_ERROR_RETRY_ATTEMPTS = 2
// A missing stream or an unsupported format will not fix itself, so it gets only a couple of
// attempts (a stale token or a short server outage can still recover); network errors get the full backoff.
@OptIn(UnstableApi::class)
internal fun maxRetryAttemptsFor(error: PlaybackException): Int {
val httpCode = (error.cause as? HttpDataSource.InvalidResponseCodeException)?.responseCode
val permanent = when (error.errorCode) {
PlaybackException.ERROR_CODE_IO_BAD_HTTP_STATUS ->
httpCode != null && httpCode in 400..499 && httpCode != 408 && httpCode != 429
PlaybackException.ERROR_CODE_IO_FILE_NOT_FOUND,
PlaybackException.ERROR_CODE_IO_CLEARTEXT_NOT_PERMITTED,
PlaybackException.ERROR_CODE_PARSING_CONTAINER_UNSUPPORTED,
PlaybackException.ERROR_CODE_PARSING_MANIFEST_UNSUPPORTED,
PlaybackException.ERROR_CODE_DECODING_FORMAT_UNSUPPORTED -> true
else -> false
}
return if (permanent) MAX_PERMANENT_ERROR_RETRY_ATTEMPTS else MAX_RETRY_ATTEMPTS
}
/** What the UI shows for the session's player; [errorMessage] turns a playback error into text. */
internal fun deriveState(p: Player, errorMessage: (PlaybackException) -> String): PlayerState {
p.playerError?.let { return PlayerState.Error(errorMessage(it)) }
return when (p.playbackState) {
Player.STATE_READY -> if (p.playWhenReady) PlayerState.Playing else PlayerState.Paused
Player.STATE_BUFFERING, Player.STATE_ENDED ->
if (p.playWhenReady) PlayerState.Connecting else PlayerState.Paused
else -> when {
p.mediaItemCount == 0 -> PlayerState.Idle
p.playWhenReady -> PlayerState.Connecting
else -> PlayerState.Paused
}
}
}