Ubuntu – “Failed to open audio output” error when trying to send audio from mpd to pulseaudio

mpdpulseaudiosound

When I try to play mpd through pulseaudio, mpd.log reports:

pulse_output: Failed to enable "Pink ROCKI" [pulse]: pa_context_connect() has failed: Connection refused

  • Pulse seems to be working fine since pacmd list-sinks shows all my pulse sinks, meb browser audio plays fine through the default sink, and VLC can play sound through any of them.
  • mpd works fine if I send the output through alsa instead of pulse
  • Both mpd and pulseaudio are running under my account (fred).
  • fred, mpd, and pulseaudio are all members of the pulse, pulse-access, and audio groups.
  • I have even tried this with different builds of pulseaudio: the 4.0 that comes with Ubuntu 14.04, the experimental RAOP2 built with pulse 4.0, and the experimental RAOP2 built with pulse 5.0. mpd will not work with any of them.

My mpd.conf file is vanilla; the pulse section is the generic:

    audio_output { 
    type "pulse" 
    name "General Pulse" 
    }

Though as laugeo suggests below, I have also tried specifying an individual sink, both by name:

audio_output {
    type        "pulse"
    name        "ROCKI"
    sink        "raop_output.ASAP1826T.local"
}

and by index #:

audio_output {
    type        "pulse"
    name        "ROCKI"
    sink        "4"
}

What should I try next? Could configuring pulseaudio to run as a system service help?

This is on Ubuntu 14.04, pulseaudio 4.0 (and 5.0), Music Player Daemon 0.18.7

Update: I found this blog where the blogger Louiz seems to have solved the same (or very similar) problem:

MPD apparently tries to spawn a new pulseaudio process, as my own
user, and it fails because it is already running. I could not
understand why it could not just use the existing pulseaudio instance,
without trying to spawn a new one.

I understood thanks to this post.

MPD could not find the already-running pulseaudio instance because the
environment variable XDG_RUNTIME_DIR was not set, and thet’s where
pulseaudio stores its files (the socket, the process pid, etc).

I tried his solution:

creating a new file /etc/systemd/system/mpd.service.d/fixenv.conf
containing:

[Service] Environment=XDG_RUNTIME_DIR=/run/user/1000

(Replace 1000 by the UNIX id of your user.)

I rebooted and XDG_RUNTIME_DIR was set correctly, and pulse put its pid file in the /run/user/1000/pulse/ directory (my user id is 1000), but I still get the same error from mpd:

pulse_output: Failed to enable "ROCKI" [pulse]: pa_context_connect() has failed: Connection refused

I'm learning more about linux every day, but sockets, process pids, systemd (isn't Ubuntu supposed to use upstart? systemd seems to be running and launching things), etc. are all above my pay grade right now – I'm lost in the woods.

Best Answer

Finally, better than pulseaudio daemon mode, the best solution is there: it is about running mpd as yourself (and not mpd user) so you have no problem to use your own session pulseaudio.

A different answer , but tested and also working:
configure pulseaudio to run as "daemon mode" instead of the standard "user mode"

  • in a terminal, run
    sudo gedit /etc/init/pulseaudio.conf
    then uncomment line 20 removing # to get:
    start on runlevel [2345]
    save and quit gedit.
  • Run this 2 commands in a terminal:
    sudo usermod -aG pulse,pulse-access mpd
    sudo usermod -aG pulse,pulse-access your-login
    In the 2nd command, replace "your-login"

Now reboot: hopefully , it will work.

Note: For info, this page lists reasons why you must avoid pulseaudio daemon mode, but i find it ok on a single user pc (especially I see no more cpu/memory usage than user mode).