On my desktop i have a mute key on the keyboard, and it shows up in X11 as a special key. From there i have set the XFCE keyboard manager to run some ALSA commands on pressing it to toggle the mute state of the master volume control.
Works fine, but then i do not have any transitory (USB) audio devices.
I suspect the lid close can be bound similarly (but these days you have the whole crapola of powerkit/powerd depending on systemd-logind handling the gruntwork, so who really knows).