Yes, I use Goodlock for many things. And the Routines module as others said is great. Much easier than a complicated app like tasker, though that can do infinitely more.
Go into Samsung store app, download Goodlock. Tap on Routines. Tap on the + symbol, bottom right. Type a name for your routine, e.g., headphone connect. Tap "add condition". Scroll down to "Phone" and tap "headphones connection". The condition should default to headphone connected, but you'll see a dual arrow on the right so you can change the condition to headphones disconnected. You'll want headphones connected.
Tap on "add action", scroll down to actions and tap on "launch an app". An app list appears and select an app. Then press save at the top.
Don't forget 4 things.
1. You can do the same as I have said but in reverse so that you can nominate to launch an app when the headphones are disconnected. Just create the above routine but instead of "headphones connected", tap the dual arrow and select "headphones disconnected.
2. You can toggle this routine on and off. If you know you're going to use headphones for YouTube and don't want Play music launching, you can go into routines and toggle your headphone routine off.
3. You can add a widget to your homescreen for the entire Routine module. Or, you can select individual routines and add a widget. So you can add the headphones routine specifically to homescreen and tap that on and off.
4. You can add other actions to your condition to headphone connect. Tap on action and you can add more actions than just launching a specific app when the headphone connects. You can select screen brightness , set volume, set do not disturb etc. So multiple things can be done with one condition of connecting headphones.