如何将 Docker 容器中的音频暴露给 Mac? [英] How to expose audio from Docker container to a Mac?

查看:50
本文介绍了如何将 Docker 容器中的音频暴露给 Mac?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道可以通过在 Linux 主机系统上使用脉冲音频但是 paprefs 是为 linux 而不是为 mac 构建的.

I know it's possible by using pulse audio on a Linux host system But paprefs is built for linux not mac.

推荐答案

Docker-for-Mac VM 没有任何声音直通设备,所以从这个角度没有任何东西可以利用.相比之下,virtualbox 或 vmware fusion VM 确实有能力做直通音频.

The Docker-for-Mac VM doesn't have any sound passthrough device, so there isn't anything that you could take advantage of from that angle. By contrast, a virtualbox or vmware fusion VM does have the ability to do passthrough audio.

我能够使用以下命令安装 pulseaudio 并在 OSX 上工作:

I was able to get pulseaudio installed and working on OSX with the following command:

brew install pulseaudio

我能够通过运行以下命令来验证这是否有效,并且听到了我的扬声器发出的声音:

I was able to verify this worked by running the following, hearing sound come out of my speakers:

paplay cockatiel.wav

我的下一步是找到具有 paplay 副本的图像.我发现了 jess/pulseaudio,它似乎是一个脉冲音频 服务器,但我应该也可以将它用作客户端.

My next step is to find an image that has a copy of paplay. I found jess/pulseaudio, which appears to be intended to be a pulseaudio server, but I should be able to use it as a client as well.

我在 Archlinux Wiki 上找到了以下关于设置 pulseaudio 网络声音的指南:https://wiki.archlinux.org/index.php/PulseAudio/Examples#PulseAudio_over_network

I found the following guide on the Archlinux Wiki discussing setting up pulseaudio network sound: https://wiki.archlinux.org/index.php/PulseAudio/Examples#PulseAudio_over_network

通过执行以下操作,我能够使其适应这种情况.我在我的 Mac 上编辑了 /usr/local/Cellar/pulseaudio/9.0/etc/pulse/default.pa,并取消了以下两行的注释:

I was able to adapt it to this situation by doing the following. I edited /usr/local/Cellar/pulseaudio/9.0/etc/pulse/default.pa on my mac, and uncommented the following two lines:

load-module module-esound-protocol-tcp
load-module module-native-protocol-tcp

我在我的 Mac 上重新运行了 paplay cockatiel.wav 以确保我的更改仍然有效.如果我打错了,pulseaudio 守护进程似乎按需启动,并将其投诉传回 paplay 以打印在我的屏幕上.我对 default.pa 的这些更改仍然有意见,所以我对我的更改没有破坏任何东西感到满意.

I reran paplay cockatiel.wav on my mac to make sure my changes still worked. the pulseaudio daemon seems to start on demand, and passes its complaints back to paplay to be printed on my screen if I made a typo. I still have sound with those changes to default.pa, so I'm satisfied that my changes didn't break anything.

接下来,我在这样的容器中运行 pulseaudio 客户端:

Next, I ran the pulseaudio client in a container like this:

docker run --rm -v $HOME:$HOME -w $HOME -it 
  -e PULSE_SERVER=192.168.10.23 
  -e HOME=$HOME --entrypoint paplay 
  jess/pulseaudio $HOME/cockatiel.wav

它的作用是将我的本地主目录作为一个卷运行一个容器.这有两个目的.首先是我的 cockatiel.wav 位于 $HOME 中.第二个是因为客户端和服务器都需要具有相同 ~/.config/pulse/cookie 文件的副本(根据该 archlinux wiki 指南).

What this does is run a container with my local home directory as a volume. This serves two purposes. The first is the fact that my cockatiel.wav is located inside $HOME. The second is because both the client and the server need to have a copy of the same ~/.config/pulse/cookie file (per that archlinux wiki guide).

PULSE_SERVER 环境变量是我的 OSX 主机的 en0 IP 地址,所以 paplay 知道要连接到什么.

The PULSE_SERVER environment variable is the en0 IP address of my OSX host, so paplay knows what to connect to.

HOME 环境变量是必需的,因此 paplay 可以找到相同的 ~/.config/pulse/cookie 文件.

The HOME environment variable is necessary so paplay can find the same ~/.config/pulse/cookie file.

我能够通过 pulseaudio 播放在我的 docker-for-mac 上运行的容器中的声音.

I was able to play sound from a container running on my docker-for-mac via pulseaudio.

只要你让 ~/.config/pulse/cookie 文件出现在正确的位置,你应该能够播放声音.您不必使用主机卷来完成此操作 - 您也可以执行docker cp",甚至将其 COPY 到图像中.

As long as you get the ~/.config/pulse/cookie file to appear in the correct location, you should be able to play sound. You don't have to use a host volume to accomplish this-- you could also do a 'docker cp', or even COPY it into an image.

这篇关于如何将 Docker 容器中的音频暴露给 Mac?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆