androidx / media

Jetpack Media3 support libraries for media use cases, including ExoPlayer, an extensible media player for Android
https://developer.android.com/media/media3
Apache License 2.0
1.75k stars 421 forks source link

Inconsistent Volume Control When Using FLAG_AUDIBILITY_ENFORCED #1695

Open rphm opened 3 months ago

rphm commented 3 months ago

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

@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:

Accurate Stream Reflection:

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:

Stream Inconsistencies:

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)

Media

Any audio file from the demo

rphm commented 3 months ago

Crossref: https://issuetracker.google.com/u/1/issues/363916146