Play mp3 in BalenaOS docker image running on Raspberry PI 4

I’m trying to play an mp3 file from within a balenaos docker image on a Raspberry Pi 4 Model B through the audio jack.

Here is the dockerfile:

FROM balenalib/raspberrypi4-64-python

RUN apt-get update && \
    apt-get install -y \
    gcc \
    alsa-utils \
    libsdl-image1.2-dev \
    libsdl-mixer1.2-dev \
    libsdl-ttf2.0-dev \
    libsdl1.2-dev \
    libsmpeg-dev \
    libportmidi-dev \
    ffmpeg \
    libswscale-dev \
    libavformat-dev \
    libavcodec-dev \
    wget \
    mplayer && \
    rm -rf /var/lib/apt/lists/* # clean up cache to reduce layer size

RUN wget https://file-examples.com/storage/fe6dd1af7b651708a98c512/2017/11/file_example_MP3_700KB.mp3 -O test.mp3

CMD ["mplayer", "-ao", "alsa", "test.mp3"]

And here is my docker-compose.yml file:

version: "2"

services:
  music:
    build: .
    privileged: true
    devices:
      - "/dev/snd:/dev/snd"

This is what I get in the logs:

[Logs]    [2023-09-30T13:47:32.467Z] [music] MPlayer 1.4 (Debian), built with gcc-10 (C) 2000-2019 MPlayer Team
[Logs]    [2023-09-30T13:47:32.470Z] [music] do_connect: could not connect to socket
[Logs]    [2023-09-30T13:47:32.471Z] [music] connect: No such file or directory
[Logs]    [2023-09-30T13:47:32.471Z] [music] Failed to open LIRC support. You will not be able to use your remote control.
[Logs]    [2023-09-30T13:47:32.471Z] [music] 
[Logs]    [2023-09-30T13:47:32.471Z] [music] Playing test.mp3.
[Logs]    [2023-09-30T13:47:32.471Z] [music] libavformat version 58.45.100 (external)
[Logs]    [2023-09-30T13:47:32.479Z] [music] Audio only file format detected.
[Logs]    [2023-09-30T13:47:32.479Z] [music] Load subtitles in ./
[Logs]    [2023-09-30T13:47:32.481Z] [music] ==========================================================================
[Logs]    [2023-09-30T13:47:32.481Z] [music] Opening audio decoder: [mpg123] MPEG 1.0/2.0/2.5 layers I, II, III
[Logs]    [2023-09-30T13:47:32.482Z] [music] AUDIO: 44100 Hz, 2 ch, s16le, 160.0 kbit/11.34% (ratio: 20000->176400)
[Logs]    [2023-09-30T13:47:32.483Z] [music] Selected audio codec: [mpg123] afm: mpg123 (MPEG 1.0/2.0/2.5 layers I, II, III)
[Logs]    [2023-09-30T13:47:32.483Z] [music] ==========================================================================
[Logs]    [2023-09-30T13:47:32.490Z] [music] [AO_ALSA] alsa-lib: pcm_hw.c:1715:(snd_pcm_hw_open) open '/dev/snd/pcmC0D0p' failed (-524): Unknown error 524
[Logs]    [2023-09-30T13:47:32.491Z] [music] [AO_ALSA] Playback open error: Unknown error 524
[Logs]    [2023-09-30T13:47:32.491Z] [music] Failed to initialize audio driver 'alsa'
[Logs]    [2023-09-30T13:47:32.491Z] [music] Could not open/initialize audio device -> no sound.
[Logs]    [2023-09-30T13:47:32.491Z] [music] Audio: no sound
[Logs]    [2023-09-30T13:47:32.492Z] [music] Video: no video
[Logs]    [2023-09-30T13:47:32.492Z] [music] 
[Logs]    [2023-09-30T13:47:32.492Z] [music] 
[Logs]    [2023-09-30T13:47:32.492Z] [music] Exiting... (End of file)

Any ideas what I’m doing wrong?

Hi,
Have you tried adding the ENV UDEV=1 to your Dockerfile?
Most times when there are issues with hardware, that’s the solution :wink:
See here for reference.

You may also want to have a look at the audio project for reference; that one is also used for balena sound

Thanks for the suggestion, but sadly that doesn’t seem to have fixed it. I think you’re right though that I should include that variable. I’ll take a look at the audio example.

Have you ensured the audio=on dtparam has been set? It can be done in balenaCloud like this:


Under ‘Configuration’ on the fleet or device.