macOS routes the media key to whatever it last remembers, so YouTube keeps playing and Apple Music opens. SmartPause finds the app actually outputting audio and pauses that. Two apps playing? One press switches, double press plays/pauses. Free, open source, menu bar only.
Hi PH. I made SmartPause because play/pause on my Mac kept opening Apple Music instead of pausing YouTube. It catches the key, asks Core Audio which app is outputting sound right now and sends the command there. Free, MIT, no analytics, one permission (Accessibility). Happy to answer anything about how it works.
SmartPause