fix(alarm): apply the configured fraction to the ring volume override
overrideMediaVolumeForRing parsed and logged the fraction argument but called the no-arg override, which always forced STREAM_MUSIC to the device maximum. On-device logcat confirmed it: the Dart side sent fraction=0.5 yet the stream was set to index=30 of 30. Combined with the player now ramping to its full range, the ring peaked at 100% of the device maximum instead of the configured 50%. The override now sets the stream to round(max * fraction), clamped to at least 1 so rounding can never mute the ring. With fraction=0.5 the stream caps at half the device maximum and the player ramps up to that cap, so the ring peaks at the configured level and the opening buffer click drops to the configured fraction rather than full scale.
This commit is contained in:
@@ -228,7 +228,7 @@ class MainActivity : AudioServiceActivity() {
|
||||
"overrideMediaVolumeForRing" -> {
|
||||
val fraction = call.argument<Number>("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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user