Does anyone know why audio wouldn't run through a cron job even though it runs perfectly fine when run through the command line. I have a python script that plays audio through a bluetooth speaker and when I run it on the command line (python helper.py) it plays fine, but running it through cron doesn't seem to work.
Extra details:
I am doing this on a Raspberry Pi that I have connected to a bluetooth speaker. I have a display connected to the raspberry pi (not doing it headless but that is the end goal)
Here is my test code for just the audio
import pygame
from pygame import mixer
def playFile(filePath):
pygame.mixer.init()
pygame.mixer.music.load(filename)
pygame.mixer.music.play()
print('I am here')
while pygame.mixer.music.get_busy():
continue
playFile('/home/pi/AlarmClock/alarm2.ogg')
This includes the definition of my audio playback function and the actual call.
Let me know if I can provide any more information to clarify this.
EDIT 1:
I have taken into account some of the suggestions and have modified my code a little bit.
My crontab now looks like the following
* * * * * /usr/bin/python /home/pi/AlarmClock/helper.py > /home/pi/AlarmClock/output.out
This just sends the output of the python script to the file output.out
The helper.py file was also updated
import sys
import pygame
from pygame import mixer
#fp = open('/home/pi/AlarmClock/erurfile.txt', 'a')
#sys.stdout = sys.stderr = fp
print('at the top')
# Playing media files
def playFile(fileName):
pygame.mixer.init()
pygame.mixer.music.load(fileName)
pygame.mixer.music.play()
print('in the method')
while pygame.mixer.music.get_busy():
continue
print('about to run method')
playFile('/home/pi/AlarmClock/alarm2.ogg')
print('finished running method')
EDIT SOLVED!: This other question completely solved the issue. Audio doesn't play with crontab on Raspberry Pi