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
@@ -25,8 +25,6 @@ data class AudioEffectsState(
val currentPreset: Int = -1,
val loudnessAvailable: Boolean = false,
val loudnessEnabled: Boolean = false,
val loudnessGainMillibel: Float = 0f,
val loudnessMaxGainMillibel: Float = 2000f,
val dynamicsProcessingAvailable: Boolean = false,
val dynamicsProcessingEnabled: Boolean = false
)
@@ -52,7 +50,10 @@ class AudioEffectsEngine(context: Context, audioSessionId: Int) {
private var equalizerEnabled = prefs.getBoolean(KEY_EQ_ENABLED, false)
private var loudnessEnabled = prefs.getBoolean(KEY_LOUDNESS_ENABLED, false)
private var loudnessGain = prefs.getInt(KEY_LOUDNESS_GAIN, 0).coerceIn(0, MAX_LOUDNESS_GAIN)
// There is no gain slider, so the switch applies a fixed, moderate boost. A gain of 0 (the old
// default) made the switch do nothing.
private val loudnessGain = prefs.getInt(KEY_LOUDNESS_GAIN, DEFAULT_LOUDNESS_GAIN)
.takeIf { it > 0 }?.coerceAtMost(MAX_LOUDNESS_GAIN) ?: DEFAULT_LOUDNESS_GAIN
private var dynamicsEnabled = prefs.getBoolean(KEY_DYNAMICS_ENABLED, false)
init {
@@ -178,7 +179,6 @@ class AudioEffectsEngine(context: Context, audioSessionId: Int) {
currentPreset = if (eqAvailable) currentPresetIndex else -1,
loudnessAvailable = loudnessEnhancer != null,
loudnessEnabled = loudnessEnhancer != null && loudnessEnabled,
loudnessGainMillibel = if (loudnessEnhancer != null) loudnessGain.toFloat() else 0f,
dynamicsProcessingAvailable = dynamicsProcessing != null,
dynamicsProcessingEnabled = dynamicsProcessing != null && dynamicsEnabled
)
@@ -233,17 +233,6 @@ class AudioEffectsEngine(context: Context, audioSessionId: Int) {
} catch (_: Exception) {}
}
fun setLoudnessGain(gainMillibel: Float) {
val le = loudnessEnhancer ?: return
val gain = gainMillibel.coerceIn(0f, MAX_LOUDNESS_GAIN.toFloat()).toInt()
if (gain == loudnessGain) return
try {
le.setTargetGain(gain)
loudnessGain = gain
prefs.edit { putInt(KEY_LOUDNESS_GAIN, gain) }
} catch (_: Exception) {}
}
fun setDynamicsProcessingEnabled(enabled: Boolean) {
val dp = dynamicsProcessing ?: return
try {
@@ -265,6 +254,7 @@ class AudioEffectsEngine(context: Context, audioSessionId: Int) {
private companion object {
const val TAG = "AudioEffectsEngine"
const val MAX_LOUDNESS_GAIN = 2000
const val DEFAULT_LOUDNESS_GAIN = 600
const val KEY_EQ_ENABLED = "eq_enabled"
const val KEY_PRESET = "eq_preset"
const val KEY_BAND_PREFIX = "eq_band_"