Inconsistent Volume Control When Using FLAG_AUDIBILITY_ENFORCED
@marcbaechinger is already working on this.
Since Sep 8, 2024.
- Dominant language
- Java
- Stars
- 3k
- Forks
- 955
- Avg merge
- 12d 14h
- Merged PRs (30d)
- 2
Description
Version
Media3 main branch
More version details
SHA-1: https://github.com/androidx/media/commit/c35a9d62baec57118ea898e271ac66819399649b
Bump media3 version to 1.4.1
Devices that reproduce the issue
- xgody X18 - SDK 29 (physical)
- moto g04s - SDK 34 (physical)
- Samsung S24 - SDK 34 (physical)
- Pixel 8 pro - SDK 34 (emulated)
- Pixel 8 pro - SDK 35 (emulated)
Devices that do not reproduce the issue
No response
Reproducible in the demo app?
Yes
Reproduction steps
Modify 'demo-session-service' -> DemoPlaybackService.kt
- Adding custom AudioAttributes with
.setFlags(C.FLAG_AUDIBILITY_ENFORCED) - Test volume behavior for
C.USAGE_ALARMandC.USAGE_MEDIA
@OptIn(UnstableApi::class) private fun initializeSessionAndPlayer() {
Log.i("DemoPlaybackService", "initializeSessionAndPlayer called")
val musicAudioAttributes = AudioAttributes.Builder()
.setUsage(C.USAGE_ALARM)
//.setUsage(C.USAGE_MEDIA)
.setFlags(C.FLAG_AUDIBILITY_ENFORCED) // add this
.build()
val player =
ExoPlayer.Builder(this)
.setAudioAttributes(musicAudioAttributes, /* handleAudioFocus= */ false)
.build()
player.addAnalyticsListener(EventLogger())
mediaLibrarySession =
MediaLibrarySession.Builder(this, player, createLibrarySessionCallback())
.also { builder -> getSingleTopActivity()?.let { builder.setSessionActivity(it) } }
.build()
}
Expected result
Volume Control Alignment:
- The device's volume buttons should directly adjust the audio stream specified by the
setUsage()method in the AudioAttributes. - For example, if
setUsage(AudioAttributes.USAGE_ALARM)is set, the volume buttons should control the "Alarm" volume stream exclusively. Similarly, ifsetUsage(AudioAttributes.USAGE_MEDIA)is set, the buttons should control the "Media" volume stream.
Accurate Stream Reflection:
- The system should consistently and accurately reflect the current audio stream being used, as defined by the
setUsage()method.
In the system's UI or volume controls, the active/visible volume stream should correspond to the one set in the AudioAttributes. - For instance, if
USAGE_ALARMis set, the system should indicate that the "Alarm" stream is active, and ifUSAGE_MEDIAis set, the "Media" stream should be shown as active.
Actual result
Impact of FLAG_AUDIBILITY_ENFORCED on Audio Behavior
This flag can lead to unexpected audio behavior on certain devices and Android SDK versions.
Key Issues
Volume Control Issues:
- Volume may be locked to a specific stream (e.g., "Ring") or become completely uncontrollable.
- Volume buttons may control the wrong stream.
- Volume may be fixed at maximum and unchangeable.
Stream Inconsistencies:
- The actual audio stream used may not match the intended one (e.g.,
USAGE_ALARMusing "Ring"). - Certain streams may not be visible in the system UI volume controls.
| SDK Version | Device | Observations |
|---|---|---|
| 29 | xgody X18 | Volume always controlled by "Ring" stream, regardless of intended usage |
| 34 | Samsung S24 | Volume uncontrollable via buttons or system settings |
USAGE_ALARM & USAGE_MEDIA at max volume, unchangeable |
||
| 34 | Pixel 8 Pro (emulated) | USAGE_ALARM at max volume, unchangeable |
USAGE_MEDIA works as expected |
||
| 35 | Pixel 8 Pro (emulated) | USAGE_ALARM: Volume buttons control "Media" stream, not "Alarm" |
USAGE_MEDIA: No sound, volume unchangeable |
Additional Notes (SDK 29)
- See also: https://stackoverflow.com/questions/44855786/audioattributes-flag-audibility-enforced-and-volume
"This Stack Overflow post highlights that
FLAG_AUDIBILITY_ENFORCEDcan unexpectedly cause media playback to be controlled by the system ringer volume instead of the media volume. This behavior is inconsistent across devices and can lead to user confusion. It's recommended to avoid this flag unless absolutely necessary due to its unpredictable effects."
Media
Any audio file from the demo
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.