KeyMapper icon indicating copy to clipboard operation
KeyMapper copied to clipboard

remap volume buttons when media is playing and screen is off

Open sds100 opened this issue 6 years ago • 2 comments

  • only works for short press and double press. Long press required a permission that can only be granted in ADB. This permission was removed in Android 11.

  • Newer versions of Android require a work around that plays some audio so your app is registered as a media player.

  • [ ] must grant the app DND access for it to set the volume to the previous position

  • [ ] make it very very clear that this only works when media is playing.

  • [ ] test on KitKat and older. I'm not sure when changing the volume for media while the screen is off was added.

  • [ ] receive the "android.media.VOLUME_CHANGED_ACTION" broadcast from AudioManager.

  • [ ] revert the volume to the previous value if the button is remapped.

sds100 avatar Nov 02 '20 11:11 sds100

You can use MediaSession with custom VolumeProvider as here. There are some subtleties. You will need to ensure that another media session does not become the recipient of volume key press events, otherwise you need to make the session active again. (example) On Android 14+, you need to make sure that there is an application that played the sound last and was not killed (or it had AudioAttributes.USAGE_MEDIA), otherwise you need play it yourself. (example + fix)

hufrea avatar Feb 15 '25 13:02 hufrea

solved by pro mode #1394

jambl3r avatar Oct 19 '25 10:10 jambl3r