AutoMute is a macOS menu bar app that mutes the sound when headphones get disconnected, Mac goes to sleep, or when it gets locked/enters screen saver.
As long as AutoMute is running, whenever your Mac goes to sleep, gets locked, or a pair of headphones gets disconnected - the sound is automatically muted.
Works with Bluetooth headphones as well.
This is meant to prevent those embarrassing moments when you're arriving at your perfectly silent office or school, opening up your mac, only to find out your beloved "Best of Shakira" playlist is still playing at full volume from last night.
It will also prevent the classic "walk too far away with your Bluetooth headset" scenario, where again, your Mac will take over the sound and allow your co-workers to bask in your embarrassment.