Loading page...Загружаем страницу...正在加载页面...
AtlasRepo

The macOS play/pause key goes to the wrong app, so I built a router for it

说明与用途

SmartPause 在 macOS 上拦截播放/暂停媒体键,并将其转发给真正输出声音的应用程序,避免意外打开 Apple Music。

问题

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.

结果

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.

工作流

  • 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.

证据

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.

来源

  • 记录日期: 2026-09-18T19:15:24.427Z