androidx / androidx/media

Inconsistent Volume Control When Using FLAG_AUDIBILITY_ENFORCED

Open
#1,695 1 comment 1 reaction 1 assignee View on GitHub

@marcbaechinger is already working on this.

Since Sep 8, 2024.

bug needs triage
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_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 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_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"
    USAGE_MEDIA: No sound, volume unchangeable

Additional Notes (SDK 29)

"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."

Media

Any audio file from the demo

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.