Ubuntu – Playing audio stops in Unity when I switch user

16.04soundunity

My setup

System 76 Kudu-Pro laptop with Ubuntu 16.04 LTS that I installed and keep regularly upgraded every few days. I am using Unity as my primary desktop with 12 users I frequently switch among to do specific tasks and for security isolation.

The problem

I start music playing then switch user and the music stops. This even happens if I ssh to localhost, unset the DISPLAY environment variable, become root, and play the music by a command (mplayer) from this session. I first encountered this problem on 14.04 LTS which was the first version I was using Unity and came pre-installed on the laptop. when switch user back to the same user that started playing the music, the music resumes. this also applies to audio played by the browser, such as Youtube videos.

My Question

How can I play music that keeps on playing (in a virtual background?) when I switch user? Also: Is PulseAudio involved in this?

Considerations

If the solution needs root or sudo access, I can do that. If the solution needs some small-scale programming, I can do that in C and/or Python. I have been thinking that one possible workaround is to get a USB audio device and hope this 2nd audio device can be made to work.

Best Answer

Running PulseAudio system-wide

With running PulseAudio in system-wide mode any audio that an authorized user started will play in the background as long as this user is logged in.

Before you proceed please read the following on why usually running system-wide is not a good idea:

Nevertheless we can do that if we want to. Only authorized accounts will then be able to control volume or start another instance of sound output.

Preparing the system for running Pulseaudio in system-wide mode

Note down all changes you had made to be able to revert them in case you don't like the outcome (I tested all this in a virtual machine).

  1. Disable autospawn of pulseaudio for the user that will control audio

    echo autospawn = no > $HOME/.config/pulse/client.conf
    

    This prevents auto-spawning of pulseaudio for the given $HOME accounts. Settings here will override the system-wide settings in /etc/pulse which of course can be made alternatively.

  2. Stop a running instance of pulseaudio in user mode

    pulseaudio -k
    
  3. Prepare users and groups needed (in case they do not yet exist)

    sudo addgroup --system pulse
    sudo adduser --system --ingroup pulse --home /var/run/pulse pulse
    sudo addgroup --system pulse-access
    sudo adduser pulse audio
    
  4. Allow user(s) to access the system wide PulseAudio

    sudo adduser <username> pulse-access
    

    Only users added to pulse-access will be able to control volume of a playing audio.

Start pulseaudio in system-wide mode

sudo pulseaudio --system

Source and further reading: Running PulseAudio as System-Wide Daemon

Related Question