I'm new to using Raspberry Pi, so the answer could be staring me in the face and I wouldn't know it.
I have a Raspberry Pi 3 B+ with the official touchscreen display and Adafruit Speaker Bonnet + enclosed speakers. The idea was to have a media center computer that could be used with my TV or by itself. While everything works well in standalone use, there's just one hitch when connecting to the TV: audio is still coming out of the speakers instead of the TV.
I followed the setup instructions on adafruit.com and have done no other modifications to Raspbian other than just running updates.
If there's a software solution that would allow the audio to switch depending on whether HDMI is used or not, that would be fantastic. My solution for HDMI video isn't that elegant either (inline switch for the display power cable); so if I could get that handled via code as well, I would be doubly grateful.