Connecting to audio output (MPV subprocess / python webapp)

I can’t play audio with my MPV subprocess in a Docker Container.

Essentially, I have a python webapp (built with Streamlit) that works locally, but not in the cloud-environments I’ve deployed it to: Azure, Heroku, and Streamlit Cloud.

The problem seems to be with a subprocess (MPV), which can’t access the machine’s audio devices within the Docker container.

Specifically, the error occurs in line 30 of my code. I’ve included the stderr output at the end of the message.

I can list the container’s audio devices with mpv --audio-device=help:

List of detected audio devices: 
'auto' (Autoselect device) '
alsa' (Default (alsa)) 
'jack' (Default (jack)) '
sdl' (Default (sdl)) 
'sndio' (Default (sndio)

I figure I need to link the container to the machine’s audio output (like my mac’s speakers)… but I’m feeling pretty stumped here! Any advice is welcome.

Here’s the stderr output of MPV process:

ALSA lib confmisc.c:855:(parse_card) cannot find card '0'
ALSA lib conf.c:5180:(_snd_config_evaluate) function snd_func_card_inum returned error: No such file or directory
ALSA lib confmisc.c:422:(snd_func_concat) error evaluating strings
ALSA lib conf.c:5180:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directory
ALSA lib confmisc.c:1334:(snd_func_refer) error evaluating name
ALSA lib conf.c:5180:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5703:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2666:(snd_pcm_open_noupdate) Unknown PCM default\nCannot connect to server socket err = No such file or directory\nCannot connect to server request channel
jack server is not running or cannot be started\nJackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock\nJackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock\nALSA lib confmisc.c:855:(parse_card) cannot find card '0'
ALSA lib conf.c:5180:(_snd_config_evaluate) function snd_func_card_inum returned error: No such file or directory
ALSA lib confmisc.c:422:(snd_func_concat) error evaluating strings
ALSA lib conf.c:5180:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directory
ALSA lib confmisc.c:1334:(snd_func_refer) error evaluating name\nALSA lib conf.c:5180:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5703:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2666:(snd_pcm_open_noupdate) Unknown PCM default

Containers are for isolation, you can’t just access the hosts hardware.

You can try privileged to access the hardware on the host (doc) or try device to mount the soundcard into the container (doc).

If you run Linux, you could try to use PulseAudio and send audio data from container via network to the audio server on the host.