Skip to content

Improve audio focus management #4707

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
May 13, 2025
Merged

Improve audio focus management #4707

merged 12 commits into from
May 13, 2025

Conversation

bmarty
Copy link
Member

@bmarty bmarty commented May 12, 2025

Content

  • When the user starts playing a voice message / an audio file / a video: request audio focus to the system so that any other application currently playing audio will pause. This was done for Element Call but not for those use cases. First commit extract the code from Element Call to reuse it at other places
  • When Element X is currently playing voice message / audio or video file and the user starts an audio stream from another app (ex: resume Spotify playback from the notification drawer), audio focus is lost and in this case, the application pauses the current playback. For Element Call, nothing is done when the audio focus is lost.

Motivation and context

Closes #4617 and better integration with the system.

Screenshots / GIFs

No change in the UI

Tests

  • This is a challenge: try to have an audio output from Element X in the same time than audio output from every other (decent) applications. It should not be possible anymore.

Tested devices

  • Physical
  • Emulator
  • OS version(s):

Checklist

  • Changes have been tested on an Android device or Android emulator with API 24
  • UI change has been tested on both light and dark themes
  • Accessibility has been taken into account. See https://github.com/element-hq/element-x-android/blob/develop/CONTRIBUTING.md#accessibility
  • Pull request is based on the develop branch
  • Pull request title will be used in the release note, it clearly define what will change for the user
  • Pull request includes screenshots or videos if containing UI changes
  • You've made a self review of your PR

@bmarty bmarty requested a review from a team as a code owner May 12, 2025 12:52
@bmarty bmarty added the PR-Bugfix For bug fix label May 12, 2025
@bmarty bmarty requested review from jmartinesp and removed request for a team May 12, 2025 12:52
Copy link
Contributor

github-actions bot commented May 12, 2025

📱 Scan the QR code below to install the build (arm64 only) for this PR.
QR code
If you can't scan the QR code you can install the build via this link: https://i.diawi.com/7y3NMY

Copy link

codecov bot commented May 12, 2025

Codecov Report

Attention: Patch coverage is 28.76712% with 52 lines in your changes missing coverage. Please review.

Project coverage is 80.07%. Comparing base (525d72d) to head (87b17d4).
Report is 11 commits behind head on develop.

Files with missing lines Patch % Lines
.../android/libraries/audio/impl/DefaultAudioFocus.kt 0.00% 34 Missing ⚠️
...wer/impl/local/player/MediaPlayerControllerView.kt 22.22% 7 Missing ⚠️
...droid/features/call/impl/ui/ElementCallActivity.kt 0.00% 5 Missing ⚠️
...ediaviewer/impl/local/DefaultLocalMediaRenderer.kt 0.00% 3 Missing ⚠️
...d/libraries/mediaplayer/impl/DefaultMediaPlayer.kt 66.66% 2 Missing ⚠️
...android/libraries/mediaplayer/impl/SimplePlayer.kt 0.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #4707      +/-   ##
===========================================
- Coverage    80.11%   80.07%   -0.04%     
===========================================
  Files         2126     2129       +3     
  Lines        56303    56354      +51     
  Branches      7021     7028       +7     
===========================================
+ Hits         45107    45128      +21     
- Misses        8780     8810      +30     
  Partials      2416     2416              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Member

@jmartinesp jmartinesp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, it was relatively easy to test using split screen with EXA an another media app.

}
AudioManager.AUDIOFOCUS_LOSS,
AudioManager.AUDIOFOCUS_LOSS_TRANSIENT,
AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK -> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe ignore it for the _CAN_DUCK variant if the requester is the media viewer? https://developer.android.com/media/optimize/audio-focus#automatic-ducking

Apparently the OS will automatically duck the audio in that case, but we shouldn't do it for voice-related audio (messages, call).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reading the doc again, I think I can improve the code behavior yes. I will add more commits.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, so I was about to check the result of the audio focus requester to prevent the start of the play back, but it seems that (well, on my phone at least) when there is an active call, the audio is ducked automatically by the system. Other apps does not always prevent playback during a phone call, for instance:

  • Netflix: cannot play
  • Youtube and Spotify: can start playback, but ducked.

So without doing anything, Element will have the same behavior than Youtube and Spotify, which is probably acceptable.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me! Thanks for the changes and the documentation.

@bmarty
Copy link
Member Author

bmarty commented May 13, 2025

Thanks, it was relatively easy to test using split screen with EXA an another media app.

Yes, it's also possible to perform such tests using a video app (Youtube, Netflix, etc.) in Pip mode.

@bmarty bmarty enabled auto-merge (squash) May 13, 2025 09:21
Copy link

@bmarty bmarty merged commit 0255111 into develop May 13, 2025
29 checks passed
@bmarty bmarty deleted the feature/bma/voiceAudioFocus branch May 13, 2025 09:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR-Bugfix For bug fix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Voice messages don't integrate nicely with the Android media player
2 participants