I'm using my rpi 3B+ as a headless embedded device. It has a USB speaker which I want to use for audio output.

What I've done

Having been through a number of SE questions, I've got a new alsa conf file setting the device priorities so that the USB output is card 0. This means that if I boot the pi and interact - e.g. aplay test.wav then the USB speaker fires correctly. The same is true if I run a python script which outputs sound using pygame.mixer.

The problem

However, if I set that python script to run at startup (I've tried .rclocal, /etc/profile, cron job, and even .bashrc - though I know the last one is strongly discouraged) the audio doesn't sound. I can't tell if it's coming through the HDMI port (I don't have audio on my monitor) or if it's simply not firing. The python script loops and plays sound regularly, so it isn't just that the device hasn't initialised yet.

What's oddest is that if I use /etc/profile, I can see the script running when I boot. If I stop it with Ctrl-C and immediately run it again from the command line, the sound plays just fine.

I'm guessing this is something to do with the user that the startup script runs as, or the order in which sound config is loaded? Can anyone help me? (or suggest another way to run the script that might work)

  • No idea if this will help - have you tried adding a delay of a few seconds to your job before trying to play the sound?
    – user115418
    Commented Dec 14, 2020 at 14:20
  • Yep - tried that (in a very hacky way - with a launcher script that waits a few seconds then calls the original script). Same results.
    – almcnicoll
    Commented Dec 14, 2020 at 17:06

1 Answer 1


I'm far from satisfied with this, but I managed to fix it with the following steps:

  1. Run launcher script
  2. Launcher script uses os.system() to call aplay -l and then aplay somewavfile.wav
  3. Launcher script waits a few seconds with sleep()
  4. Launcher script calls main script, which initialises pygame and uses it for sound

It's not pretty, but it works - well enough for me at least. I'm not going to accept this answer initially though, in case someone can offer something better!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.