diff --git a/android/app/src/main/kotlin/es/freetimelab/pluriwave/MainActivity.kt b/android/app/src/main/kotlin/es/freetimelab/pluriwave/MainActivity.kt index c6ec656..c92fc98 100644 --- a/android/app/src/main/kotlin/es/freetimelab/pluriwave/MainActivity.kt +++ b/android/app/src/main/kotlin/es/freetimelab/pluriwave/MainActivity.kt @@ -228,7 +228,7 @@ class MainActivity : AudioServiceActivity() { "overrideMediaVolumeForRing" -> { val fraction = call.argument("fraction")?.toFloat() ?: 1.0f Log.d(tag, "alarm.channel overrideMediaVolumeForRing fraction=$fraction") - overrideMediaVolumeForRing() + overrideMediaVolumeForRing(fraction) result.success(null) } "restoreMediaVolume" -> { @@ -320,7 +320,7 @@ class MainActivity : AudioServiceActivity() { * by device volume 0. Idempotent: a second call while already overridden * is a no-op so the originally captured value is never clobbered. */ - private fun overrideMediaVolumeForRing() { + private fun overrideMediaVolumeForRing(fraction: Float) { if (mediaVolumeOverridden) { Log.d(tag, "alarm.channel overrideMediaVolumeForRing skipped (already overridden)") return @@ -329,10 +329,19 @@ class MainActivity : AudioServiceActivity() { val audioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager val current = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC) val max = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC) - audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, max, 0) + // Cap the stream at the alarm's configured fraction of the device + // maximum (not the raw max): this keeps the ring independent of the + // device's own volume (audible at 0) while "50%" means 50% of max, + // with the player ramping up to full underneath this cap. At least + // 1 so the ring is never silenced by rounding. + val target = Math.round(max * fraction.coerceIn(0f, 1f)).coerceIn(1, max) + audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, target, 0) capturedMediaVolume = current mediaVolumeOverridden = true - Log.d(tag, "alarm.channel overrideMediaVolumeForRing captured=$current max=$max") + Log.d( + tag, + "alarm.channel overrideMediaVolumeForRing captured=$current target=$target max=$max" + ) } catch (error: Throwable) { Log.e(tag, "alarm.channel overrideMediaVolumeForRing failed", error) }