feat(android): add preferConnectedAudioDevice flag to route audio to …#401
Conversation
…earphones or Bluetooth STREAM_ALARM / USAGE_ALARM (introduced in 5.4.0) forces audio to the built-in speaker by Android design, bypassing connected earphones and Bluetooth devices. This is correct for wake-up alarms but is a regression for developers who need external device routing. Adds preferConnectedAudioDevice (default false) to AlarmSettings. When true, both AudioService and VolumeService switch to STREAM_MUSIC + USAGE_MEDIA, routing to a connected earphone or Bluetooth device if present and falling back to the speaker if not. The trade-off is that the media volume slider controls the volume instead of the alarm slider. Also fixes androidStopAlarmOnTermination being absent from AlarmSettings copyWith() and props — two callers relying on equality or copyWith for this field would have seen silent incorrect behaviour. Threads the flag through the full stack: AlarmSettingsWire pigeon definition → Dart AlarmSettings model → regenerated platform bindings → Android AlarmSettings.kt (with = false default for safe deserialization of persisted alarms) → AlarmService.kt → AudioService.kt and VolumeService.kt. Fixes gdelataillade#400. Tested on: Pixel 9a, Android 16
|
Thanks @lw-manoj, the fix looks great — clean threading through every layer, backward compat is solid, and nice catch on the stale Two small things before I merge: 1. Audio focus stream consistency
2. README docsCould you add a row for Once those are in I'll merge. Thanks again for the fast turnaround! |
|
Thanks @gdelataillade for reviewing and catching the missing update. I will fix it later tonight and update the PR. |
…e flag requestAudioFocus() hardcoded USAGE_ALARM / STREAM_ALARM regardless of preferConnectedAudioDevice, causing an inconsistency where MediaPlayer used USAGE_MEDIA for playback but audio focus was still requested as alarm-typed. This affects how other apps duck or yield to the alarm. When preferConnectedAudioDevice is true, requestAudioFocus() now uses USAGE_MEDIA (API 26+) and STREAM_MUSIC (pre-26), consistent with the AudioAttributes already set on MediaPlayer. Also adds preferConnectedAudioDevice to the AlarmSettings table in README.
|
Thanks for the thorough review @gdelataillade. Both points addressed:
|
|
Thank you! Added to version |
…earphones or Bluetooth
STREAM_ALARM / USAGE_ALARM (introduced in 5.4.0) forces audio to the built-in speaker by Android design, bypassing connected earphones and Bluetooth devices. This is correct for wake-up alarms but is a regression for developers who need external device routing.
Adds preferConnectedAudioDevice (default false) to AlarmSettings. When true, both AudioService and VolumeService switch to STREAM_MUSIC + USAGE_MEDIA, routing to a connected earphone or Bluetooth device if present and falling back to the speaker if not. The trade-off is that the media volume slider controls the volume instead of the alarm slider.
Also fixes androidStopAlarmOnTermination being absent from AlarmSettings copyWith() and props — two callers relying on equality or copyWith for this field would have seen silent incorrect behaviur.
Threads the flag through the full stack: AlarmSettingsWire pigeon definition → Dart AlarmSettings model → regenerated platform bindings → Android AlarmSettings.kt (with = false default for safe deserialization of persisted alarms) → AlarmService.kt → AudioService.kt and VolumeService.kt.
Fixes #400.
Tested on: Pixel 9a, Android 16