Search code examples
androidaudio-playerbackground-serviceandroid-music-player

how to prevent clashes of music player sound and background sound of my app in android studio


I'm developing one application with background sound through out the application using background service,so problem is i'm running my app and if my music player is running - both sound clashes with each other.

My question : how to prevent clashes of music player sound and background sound of my app

actual: music clashes with each other.

expected: if player is running it must be pause and background sound of my app is only plays.


Solution

  • You must request audio focus by calling requestAudioFocus(). This informs the other app that you would like to play something and that they should stop (in the case where you request AUDIOFOCUS_GAIN as would be appropriate if you are playing background sound continuously).

    Note that requestAudioFocus requires you pass in a OnAudioFocusChangeListener - just like the other app, you should also respect when other apps request audio focus and stop your background sound.

    Note: you should also always offer the ability to turn off background sounds (and with it, any requests for audio focus). This allows users to continue to listen to their music even while in your app.