Adding custom AudioAttributes with .setFlags(C.FLAG_AUDIBILITY_ENFORCED)
Test volume behavior for C.USAGE_ALARM and C.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, if setUsage(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_ALARM is set, the system should indicate that the "Alarm" stream is active, and if USAGE_MEDIA is 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 IssuesVolume 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_ALARM using "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"
"This Stack Overflow post highlights that FLAG_AUDIBILITY_ENFORCED can 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."
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
Devices that do not reproduce the issue
No response
Reproducible in the demo app?
Yes
Reproduction steps
Modify 'demo-session-service' -> DemoPlaybackService.kt
.setFlags(C.FLAG_AUDIBILITY_ENFORCED)
C.USAGE_ALARM
andC.USAGE_MEDIA
Expected result
Volume Control Alignment:
setUsage()
method in the AudioAttributes.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:
setUsage()
method. In the system's UI or volume controls, the active/visible volume stream should correspond to the one set in the AudioAttributes.USAGE_ALARM
is set, the system should indicate that the "Alarm" stream is active, and ifUSAGE_MEDIA
is set, the "Media" stream should be shown as active.Actual result
Impact of
FLAG_AUDIBILITY_ENFORCED
on Audio BehaviorThis flag can lead to unexpected audio behavior on certain devices and Android SDK versions.
Key Issues Volume Control Issues:
Stream Inconsistencies:
USAGE_ALARM
using "Ring").USAGE_ALARM
&USAGE_MEDIA
at max volume, unchangeableUSAGE_ALARM
at max volume, unchangeableUSAGE_MEDIA
works as expectedUSAGE_ALARM
: Volume buttons control "Media" stream, not "Alarm"USAGE_MEDIA
: No sound, volume unchangeableAdditional Notes (SDK 29)
Media
Any audio file from the demo