如何将 ALSA 默认设备设置为 Docker 上的pulseaudio声音服务器?

nek*_*uuu 2 sound pulseaudio alsa docker

我想使用 PulseAudio 和 ALSA 在 Docker 上的 Ubuntu 16.04 上播放声音。但是它输出错误Device or resource busy。我认为这是因为ALSA的默认设备没有设置为PulseAudio声音服务器(注意:它在主机Ubuntu上设置为PulseAudio)。如何将默认设备更改为它?例如,我可以通过创建~/.asoundrc或修改来实现/etc/pulse/client.conf吗?

重现步骤

$ # On the host Ubuntu...
$ aplay -L | head -n9
default
    Playback/recording through the PulseAudio sound server
null
    Discard all samples (playback) or generate zero samples (capture)
pulse
    PulseAudio Sound Server
sysdefault:CARD=PCH
    HDA Intel PCH, ALC295 Analog
    Default Audio Device
$ aplay /usr/share/sounds/alsa/Front_Center.wav 
Playing WAVE '/usr/share/sounds/alsa/Front_Center.wav' : Signed 16 bit Little Endian, Rate 48000 Hz, Mono
$ paplay /usr/share/sounds/alsa/Front_Center.wav  # Success
Run Code Online (Sandbox Code Playgroud)
$ # On the Docker container...
$ sudo docker run -it --device /dev/snd ubuntu:16.04 /bin/bash
root@81af4bf99890:/# apt update
root@81af4bf99890:/# apt install alsa-base alsa-utils pulseaudio
root@81af4bf99890:/# aplay -L | head -n10
null
    Discard all samples (playback) or generate zero samples (capture)
pulse
    PulseAudio Sound Server
default:CARD=PCH
    HDA Intel PCH, ALC295 Analog
    Default Audio Device
sysdefault:CARD=PCH
    HDA Intel PCH, ALC295 Analog
    Default Audio Device
root@81af4bf99890:/# aplay /usr/share/sounds/alsa/Front_Center.wav 
ALSA lib pcm_dmix.c:1029:(snd_pcm_dmix_open) unable to open slave
aplay: main:722: audio open error: Device or resource busy
root@81af4bf99890:/# paplay /usr/share/sounds/alsa/Front_Center.wav 
Connection failure: Connection refused
pa_context_connect() failed: Connection refused
Run Code Online (Sandbox Code Playgroud)

环境

  • 主机:Ubuntu 17.04
  • Docker:17.09.0-ce

补充说明

  • 在我的环境中,Jess 的图像也不起作用。这是一个示例日志。

    $ # When I play some sounds on host, `jess/spotify` fails to play music.
    $ sudo docker run -it \
        -v /tmp/.X11-unix:/tmp/.X11-unix \
        -e DISPLAY=unix$DISPLAY \
        --device /dev/snd \
        --name spotify \
        jess/spotify
    Gtk-Message: Failed to load module "canberra-gtk-module"
    libGL error: MESA-LOADER: failed to retrieve device information
    libGL error: Version 4 or later of flush extension not found
    libGL error: failed to load driver: i915
    libGL error: failed to open drm device: No such file or directory
    libGL error: failed to load driver: i965
    [1115/043835.051841:ERROR:sandbox_linux.cc(344)] InitializeSandbox() called with multiple threads in process gpu-process.
    [1115/043840.945653:ERROR:web_plugin_impl.cc(38)] Widevine registration is not supported after context initialization
    
    (spotify:1): GLib-GIO-CRITICAL **: g_dbus_connection_send_message: assertion 'G_IS_DBUS_CONNECTION (connection)' failed
    ALSA lib pcm_dmix.c:1052:(snd_pcm_dmix_open) unable to open slave
    
    Run Code Online (Sandbox Code Playgroud)

    最后一行表示存在与 ALSA 相关的错误。(注意jess/spotify在没有其他播放声音的软件时可以播放音乐。这种情况下可能是因为声音设备不忙。)

  • 虽然我自己回答了这个问题,但我仍然想要更复杂的解决方案或详细解释需要某些选项的原因。任何意见将不胜感激。谢谢你。

nek*_*uuu 5

感谢 fsmunoz 的这个评论,我可以使用 pulseaudio 在容器上播放声音,而无需修改任何配置文件。要点如下:

这是一个工作示例:

$ sudo docker run -it \
    --device /dev/snd \
    -e PULSE_SERVER=unix:${XDG_RUNTIME_DIR}/pulse/native \
    -v ${XDG_RUNTIME_DIR}/pulse/native:${XDG_RUNTIME_DIR}/pulse/native \
    -v ~/.config/pulse/cookie:/root/.config/pulse/cookie \
    --group-add $(getent group audio | cut -d: -f3) \
    ubuntu:16.04 /bin/bash
root@9c9f7e0db4e3:/# apt update
root@9c9f7e0db4e3:/# apt install alsa-base alsa-utils pulseaudio
root@eed016c1fb61:/# aplay -L | head -n9
default
    Playback/recording through the PulseAudio sound server
null
    Discard all samples (playback) or generate zero samples (capture)
pulse
    PulseAudio Sound Server
sysdefault:CARD=PCH
    HDA Intel PCH, ALC295 Analog
    Default Audio Device
root@9c9f7e0db4e3:/# aplay /usr/share/sounds/alsa/Front_Center.wav 
Playing WAVE '/usr/share/sounds/alsa/Front_Center.wav' : Signed 16 bit Little Endian, Rate 48000 Hz, Mono
root@9c9f7e0db4e3:/# paplay /usr/share/sounds/alsa/Front_Center.wav  # Success!
Run Code Online (Sandbox Code Playgroud)

注意:仍然,我不知道为什么需要 cookie。如果没有 cookie,就会发生错误ALSA lib pulse.c:243:(pulse_connect) PulseAudio: Unable to connect: Access denied