Some 6 years ago I bought new bluetooth headphones.
Every time I'd put them on, my macbook would open apple music (I didn't even know it was installed). Every time. No way to disable it, I really tried. Stopped shy of doing some kernel stuff.
Sold that laptop, and have never touched anything apple since. Probably never will. The hardware's good, everything else is an embarrassing mess.
It's because your headphones were sending a Bluetooth "Play" command on connect (my Honda Odyssey does this as well). For anyone else with this problem, you can override this silly default in macOS using Privacy & Security > Bluetooth, adding Music, then turning off Bluetooth access for Music.
That is maybe questionable behaviour from the headphones. But the worse problem is that there is no way to change what media player is used. I don't use Apple Music, have no songs in it and no subscription. But it opens that rather than the player I do use.
I'm pretty sure I have also launched Apple Music accidentally with some keyboard button or touchbar action. For a "premium" device having to close Apple Music (effectively an ad) a few times a week is not acceptable.
Not only every time they connect, but every time they are paused or stopped too. My car has this problem. It was not possible to pause the music, it would always immediately restart play.
Sold that laptop, and have never touched anything apple since. Probably never will. The hardware's good, everything else is an embarrassing mess.
Sent from my Ubuntu.