1

I have a USB sound card connecting mic & speakers to a Raspberry Pi Zero W +

The following script plays audio fine when run from the terminal. However, when run from cron, nothing can be heard.

Script:

import simpleaudio as sa

print('playing audio')

wave_obj = sa.WaveObject.from_wave_file('/home/pi/music.wav') play_obj = wave_obj.play() play_obj.wait_done()

print('stopped')

Pi user's Cron:

@reboot sleep 5; python3 /home/pi/audio_test.py > /home/pi/audio_test.log

/home/pi/audio_test.log is created and contains the string 'playing audio', so I know the script is running, but something is obviously going wrong when it comes to actually playing the audio.

Question:

How do I get the python script to play audio at boot without an interactive shell?

PangolinPaws
  • 121
  • 4

1 Answers1

1

A question relating to Pygame and using it to play sound at boot has an answer that worked for me:

Pygame.mixer does not play sound when started in cron

There it was suggested that the script should be started "from the GUI autostart not from Cron":

sudo nano /etc/xdg/lxsession/LXDE-pi/autostart

Then add this line to the end:

@/usr/bin/python /home/pi/audio_test.py

I still don't know why cron doesn't work, but I'm happy to have found an alternative.

PangolinPaws
  • 121
  • 4