Search code examples
androidaudio

How to make background sound quieter


If you have an app like Spotify playing music in a background and try to listen to some sound in Google Chrome at the same moment, the background music becomes quieter at this very moment while Chrome playing the sound and then comes back to normal level.

I am wondering how I can do the same from my own Android app, namely, to make volume of all other applications lower for some time, but allow my application to play sounds with normal volume at the same time. I tried to find the answer, but usually all recommendations are about how to change volume globally.


Solution

  • This is a feature called audio ducking- the sound that gets quieter is said to be ducked. The way Android does it is through audio focus. Your app needs to request focus, and the other app's sound will be changed appropriately. The documentation on it is at https://developer.android.com/guide/topics/media-apps/audio-focus. The mechanism changed in Android 12, so you'll likely have to have different code for older versions and newer ones to make it all work.