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

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

我想使用 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
$ # 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

环境

  • 主机: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
    

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

  • 虽然我自己回答了这个问题,但我仍然希望得到更复杂的解决方案或详细解释某些选项需要的原因。任何评论都会受到赞赏。谢谢。

答案1

感谢 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!

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

相关内容