androidx / androidx/media

More fine grained control over MediaButton events possible (e.g. LONGPRESS)?

Open
#2,637 12 comments 1 reaction 1 assignee View on GitHub

@marcbaechinger is already working on this.

Since Jul 23, 2025.

bug
Dominant language
Java
Stars
3k
Forks
955
Avg merge
12d 14h
Merged PRs (30d)
2

Description

Hi,

I'm trying to create an app for playing audio books that has really fine grained controls via headset buttons
(MediaButtons).

Unfortunately I did not find any way to implement or receive ACTION_UP or LONGPRESS events while running in the background (app as a foreground service). The recommended way of implementing custom MediaButton behaviour is (to my knowledge) implementing

  • MediaLibrarySession.Callback with
  • override fun onMediaButtonEvent(session: MediaSession, controllerInfo: ControllerInfo, intent: Intent)

My Example project:
https://github.com/pilabor/Voice/blob/df3415bd4e57e24ada1c777d2c0997b8ed7c0342/playback/src/main/kotlin/voice/playback/session/LibrarySessionCallback.kt#L315

Unfortunately this does not offer the possibility of capturing LONGPRESS events while in background (e.g. screen off). When in foreground, a LONGPRESS delays the emitted event for 1000ms, which is also unfortunate, because combinations like KEY_DOWN, KEY_UP to e.g. run a player action next track has to be delayed for at least 1050ms to wait for a possible LONGPRESS coming after that, which makes all control patterns feel laggy (650ms would be the timing that feels right).

On old Apple iPods the following headset click patterns where implemented (CLICK means keydown + keyup, LONGPRESS means holding keydown:

  • 1 CLICK - Play / Pause
  • 2 CLICK - Next track or chapter
  • 3 CLICK - Previous track or chapter
  • LONGPRESS - read out current track title
  • 1 CLICK + LONGPRESS - fast forward as long as the button is held down
  • 2 CLICK + LONGPRESS - rewind as long as the button is held down

I can't implement these on Android with its current state and I would love to have more control over the button patterns.

Is there any way to capture raw KEY_DOWN and KEY_UPevents without ANY delay or is the current state intended behavior somehow?

Feel free to convert this into a feature request, if required.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.