According to 'user.dz' - you can trigger a script when headphones is disconnected.
when Headphone disconnects - use ACPI to trigger a script
(What code is executed when headphones are disconnected?).
In most systems if not all, ACPI can handle this event. To test that:
Run acpi_listen
Unplug & replug headphones, example output: (mic/ears share in same >jack on my laptop)
jack/headphone HEADPHONE unplug
jack/microphone MICROPHONE unplug
jack/headphone HEADPHONE plug
jack/microphone MICROPHONE plug
Put your-script.sh in /etc/acpi/
Add an event trigger file for your script in /etc/acpi/events/
event=jack/headphone HEADPHONE unplug
action=/etc/acpi/your-script.sh
Check the other files there to learn from.
You may need to restart acpid service to reload changed rules in >/etc/acpi
/events/
sudo service acpid restart
Default Headphone setting within terminal for the script
Could be done with recommendation to 'Takkat's answer from
(How can I change the default audio device from command line?)
You can control PulseAudio thoroughly through the command line using pacmd and >pactl commands. For options see man pages or the wiki at PulseAudio:
pacmd list-sinks (or pactl list short sinks) for name or index number of possible >sinks
pacmd set-default-sink "SINKNAME" to set the default output sink
pacmd set-default-source "SOURCENAME" to set the default input
pacmd set-sink-volume index volume
pacmd set-source-volume index volume for volume control (65536 = 100 %, 0 = mute; or a bit more intuitive 0x10000 = 100 %, 0x7500 = 75 %, 0x0 = 0 %)
and many many more CLI options.
This is my first answer - I hope it helps and pardon/let me know if theres anything I should've done differently.
export PULSE_RUNTIME_PATH="/run/user/<your user id>/pulse/"
This is needed because ACPI scripts run as root and pulse audio runs and the logged user. – Óscar Sep 26 '20 at 09:20action=/bin/su -c "/etc/acpi/your_script.sh" - your_username
– Óscar Sep 26 '20 at 09:33