43

I have to plugin my USB Audio adapter ( 4300054 Gigawire USB Audio Adapter) for audio input because has a combo-input-output port for voice. After I do this, I have go open Sound Settings and manually select the USB Audio adapter for Input and Output, if I do not, the system default remains selected.

Is there anyway, I can make Ubuntu to automatically select the USB Audio Adapter as the default as soon as I plug-in?

3 Answers 3

61

There is pulseaudio module-switch-on-connect and module-switch-on-port-available that enable automatic switching of an audio device on connection. See with the following command if any of these modules is already loaded:

pactl list short modules

If not, then test if you can make automatic switching work by issuing one or both of the following commmands in a terminal:

pactl load-module module-switch-on-connect
pactl load-module module-switch-on-port-available

On success add one or both of the following lines to your /etc/pulse/default.pa

load-module module-switch-on-connect
load-module module-switch-on-port-available

This will then always load these modules on every login, resp. restart of the pulseaudio daemon.

Please note that local user settings in ~/.config/pulse/default.pa override system-wide setting. If you have such a local file you will enter the above commands there.

If it still not works you may have conflicting settings in such a local default.pa. It then may worth to (temporarily) rename this file followed by a restart of the Pulseaudio server before trying above again:

mv ~/.config/pulse/default.pa ~/.config/pulse/default.pa.old
pulseaudio -k
14
  • I just re-found this answer and wish I could upvote it again. This solves the issue on Ubuntu 14.04. Oct 8, 2015 at 10:02
  • 1
    such a long time it is out there, and still not set to be loaded automatically in 16.04 :| Jul 16, 2016 at 9:54
  • just applied this to 17.04 - perfect solution and really should be default
    – fishears
    Aug 4, 2017 at 7:06
  • 6
    What if Module load failed ? Jul 21, 2019 at 18:33
  • 1
    @Scaine: hi there :) good to see you again. The module may have already been loaded (see pactl list short modules). PA spits out an error if we try to load a module twice.
    – Takkat
    Jul 29, 2020 at 19:16
28

create ~/.config/pulse/default.pa if it doesn't exist and append

.include /etc/pulse/default.pa
load-module module-switch-on-connect

This is better than editing /etc/pulse/default.pa.

Afterwards you should run pulseaudio -k && pulseaudio --start to have the changes take effect. Thanks for pointing that out lreeder

7
  • 4
    Works for me on 16.04 LTS. You also need to restart pulseaudio after creating this config. 'pulseaudio -k' from the command line will do it.
    – lreeder
    Jan 7, 2017 at 16:16
  • 4
    Why is it "better than editing /etc/pulse/default.pa"?
    – Rodrigo
    Apr 1, 2018 at 13:07
  • I used this to get my connected headphones set as default after a restart, just editing /etc/pulse/default.pa did not work. Feb 1, 2019 at 16:04
  • 2
    @Rodrigo because it is usually not a good idea to edit system-wide configuration files, which may be overwritten by an update and/or affect other users. See here: wiki.archlinux.org/index.php/PulseAudio#Configuration_files
    – han-tyumi
    Jul 1, 2019 at 16:54
  • @han-tyumi Of course! Thank you!
    – Rodrigo
    Jul 1, 2019 at 23:39
1

I tested solutions for a long time that I could find in the documentation or on forums and this was the only one that worked.

So here is a script that I created that you can add when starting a session (unfortunately not for the entire computer because PulseAudio is a service that runs independently for each user).

#!/bin/bash
index=$(pacmd list-sources | egrep 'index|ports|analog-input-headset-mic' | egrep '\*\sindex:\s+[0-9]'  | cut -d':' -f2);

acpi_listen | while IFS= read -r line;
do
    if [ "$line" = "jack/headphone HEADPHONE plug" ]
    then
       pacmd set-source-port $index analog-input-headset-mic;
    elif [ "$line" = "jack/headphone HEADPHONE unplug" ]
    then
       pacmd set-source-port $index analog-input-internal-mic;
    fi
done

There is a solution which can be found on the first link of my sources, but it does not work for all PCs unfortunately. Here are the links that allowed me to create this script

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .