The macOS play/pause key goes to the wrong app, so I built a router for it
Description and use
SmartPause intercepts play/pause media keys on macOS and forwards them to the app actually outputting sound, preventing accidental Apple Music launches.
Problem
Media keys on macOS send play/pause to the app that last registered with the MediaRemote framework, often causing the wrong app (e.g., Apple Music) to play instead of the intended source like a browser tab.
Outcome
SmartPause reliably routes play/pause media keys to the app that is actually producing sound, eliminating accidental launches of Apple Music and improving media control consistency across macOS.
Workflow
- 1. Capture NX_SYSDEFINED media key events using a CGEventTap with Accessibility permission.
- 2. Identify the key code and ensure it is the play/pause key (subtype 8).
- 3. Query Core Audio for running output processes using kAudioHardwarePropertyProcessObjectList and filter by kAudioProcessPropertyIsRunningOutput.
- 4. Resolve the responsible application for each process using the undocumented responsibility_get_pid_responsible_for_pid function.
- 5. Check each candidate app’s playback state via AppleScript (e.g., Spotify) or JavaScript in the browser (e.g., Brave) to confirm it is actively playing.
- 6. If the app is playing, send a play or pause command using AppleScript or the app’s Apple Events; otherwise, ignore the key press.
- 7. Return nil from the event tap callback to swallow the original media key event, preventing macOS from routing it to the wrong app.
Evidence
Since macOS 14.2 Core Audio exposes the list of audio processes as public API, with a property that says whether each one is outputting right now.
The whole call takes about 14 ms. That is the core of the app.
I also stopped using toggle commands entirely. playpause is a coin flip when your state is wrong; play and pause are not.
- Record date: 2026-09-18T19:15:24.427Z