Having issues in playing audio from a python program running in a container

Hi,
I am new to BalenaOS and need help with playing audio from a container.

I am trying to play an mp3 file from a python program running in my container, pushed to BalenaOS 2.48.0+rev1 and supervisor 10.8.0 running on a BalenaFin(cm3) in local mode.
And I am getting an error ‘pygame.error: No available audio device’, I am using pypi package pygame to play my audio file. Before moving to BalenaOS, I was able to run the same program play audio files with Raspbian-buster-light installed on BalenFin(cm3). Not sure what I am missing!!!

[Logs] [5/19/2020, 1:24:06 PM] [mosquitto] 1589865846: Socket error on client auto-F03E4FCE-B00D-9FDD-90B6-706FB2542667, disconnecting.
[Logs] [5/19/2020, 1:24:21 PM] Service exited ‘mercury-alerts-service sha256:9f2ef7dab94acf7927bc111db04e6a6bb67f18e2199cb2766bc4c63630ad38f1’
[Logs] [5/19/2020, 1:24:22 PM] Restarting service ‘mercury-alerts-service sha256:9f2ef7dab94acf7927bc111db04e6a6bb67f18e2199cb2766bc4c63630ad38f1’
[Logs] [5/19/2020, 1:24:06 PM] [mercury-alerts-service] setting log level %i 20
[Logs] [5/19/2020, 1:24:06 PM] [mercury-alerts-service] Traceback (most recent call last):
[Logs] [5/19/2020, 1:24:06 PM] [mercury-alerts-service] File “./service.py”, line 5, in
[Logs] [5/19/2020, 1:24:06 PM] [mercury-alerts-service] AlertsService().run()
[Logs] [5/19/2020, 1:24:06 PM] [mercury-alerts-service] File “/mercury/services/alerts_service.py”, line 42, in init
[Logs] [5/19/2020, 1:24:06 PM] [mercury-alerts-service] self.audio_player = AudioPlayer()
[Logs] [5/19/2020, 1:24:06 PM] [mercury-alerts-service] File “/mercury/services/alerts_service.py”, line 212, in init
[Logs] [5/19/2020, 1:24:06 PM] [mercury-alerts-service] pygame.mixer.init()
[Logs] [5/19/2020, 1:24:06 PM] [mercury-alerts-service] pygame.error: No available audio device

docker-compose.yml is

version: '2.1'
volumes:
    alerts-service:
services:
  mercury-alerts-service:
    build: ./alerts-service/
    network_mode: "host"
    volumes:
       - alerts-service:/app
    devices:
       - "/dev/snd:/dev/snd"
  mosquitto:
    image: "eclipse-mosquitto"
    ports:
      - "1883:1883"
    network_mode: "host"

I have asound.conf with following setting and copied to /etc/

pcm.!default {
    type hw
    card 1
}

ctl.!default {
    type hw           
    card 1
}

Dockerfile is

FROM balenalib/raspberrypi3-python:3.7

WORKDIR ./

RUN mkdir ./app \ 
RUN mkdir ./app/logs \
    && mkdir ./app/config \
    && mkdir ./app/media 

RUN install_packages python-dev gcc alsa-utils libsdl-image1.2-dev \
                     libsdl-mixer1.2-dev libsdl-ttf2.0-dev   libsdl1.2-dev \
                     libsmpeg-dev python-numpy subversion libportmidi-dev \
                     ffmpeg libswscale-dev libavformat-dev libavcodec-dev \
                     libfreetype6-dev

COPY asound.conf /etc/asound.conf
COPY requirements.txt ./
COPY .env ./app
COPY ./config ./app/config
COPY ./media ./app/media
COPY ./mercury ./mercury

RUN pip install -r requirements.txt

ENV PYTHONPATH "${PYTHONPATH}:./mercury"

COPY . .

CMD [ "python", "./service.py" ]

Hey - welcome to the forums! And good first post: lots of information right from the start.

I’ve compared your compose and docker file to ours at balenaSound and the only thing that stands out is that we run the container accessing the soundcard as privileged. Could you try setting the privileged flag on your mercury-alerts-service and trying again?

Also, I noticed that Googling the error your getting brings up quite a few other people having the same issue on non-balena version of Linux. Several threads all suggest the following:

apt-get install libsdl1.2-dev libsdl-image1.2-dev libsdl-mixer1.2-dev libsdl-ttf2.0-dev

Also worth a try.

Let us know how you get on!
Phil

1 Like

pcm.!default { type hw card 1 } ctl.!default { type hw card 1 }

Thanks @phil-d-wilson its working now

After I posted question here I tried cat /proc/asound/cards and got following

0 [ALSA ]: bcm2835_alsa - bcm2835 ALSA
                      bcm2835 ALSA

Then I tried changing asound.conf with card ss 0 and it worked, keeping all other configs as it is.

pcm.!default {
    type hw
    card 0
}

ctl.!default {
    type hw           
    card 0
}

Hey - no worries! Glad you got it working, and thanks for letting us know what fixed it. Have fun with your project.

Phil

1 Like