鍍金池/ 問答/Linux  網絡安全/ 如何讓ALSA播放在docker中可用?

如何讓ALSA播放在docker中可用?

之前pull了一個ros:kinetic-robot鏡像,這個鏡像是基于ubuntu的,然后把以前的ALSA播放wav代碼掛上去執(zhí)行

docker run -it \
--device /dev/snd:/dev/snd \
-v /media/dyan/project/projects/voice/:/data/voice/ \
ros:kinetic-robot

播放音頻報錯了,打開聲卡設備時出了問題

ALSA lib pcm_dmix.c:1029:(snd_pcm_dmix_open) unable to open slave #這是snd_pcm_open()輸出的異常
cannot open audio device default (No such file or directory) #這一行是我在err_num=snd_pcm_open()失敗后打印的,括號中的信息是snd_strerror(err_num)

查看過了一些相關問題run apps using audio in a docker container
但是仍然解決不能解決。
在容器里面apt search alsa,alsa的庫是有的,代碼在容器中也能編譯通過。

root@72b53ba14730:/# apt search alsa
Sorting... Done
Full Text Search... Done
libasound2/now 1.1.0-0ubuntu1 amd64 [installed,local]
  shared library for ALSA applications

libasound2-data/now 1.1.0-0ubuntu1 all [installed,local]
  Configuration files and profiles for ALSA drivers

libasound2-dev/now 1.1.0-0ubuntu1 amd64 [installed,local]
  shared library for ALSA applications -- development files

libgcrypt20/now 1.6.5-2ubuntu0.4 amd64 [installed,local]
  LGPL Crypto library - runtime library

libsdl1.2debian/now 1.2.15+dfsg1-3 amd64 [installed,local]
  Simple DirectMedia Layer

不知道問題出在哪里,是設備映射哪里不對嗎?

回答
編輯回答
北城荒

找到了
鏈接描述
鏈接描述

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) \
    -v /media/dyan/project/projects/voice/:/data/voice/ \
    ros:kinetic-robot

然后還需要安裝一些包

apt update && apt install alsa-base pulseaudio

嗯,目前可以正常播放了

2017年1月8日 15:24