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
However, if I set that python script to run at startup (I've tried
/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)