I'm having some trouble getting my python based gpio audio player to run on boot. It's a short piece of that plays a single wave file to its end then waits for another button press to trigger it again.
It works perfectly fine in Idle3 in the GUI and also when I run via python in terminal.
Issue is, I want to run this via CRON on boot. When I call the program to run by setting it as an executable via chmod 755 etc, I get an error as follows:
The line in code is the usual:pygame.error: Couldn't set hardware audio parameters: Success
which works perfectly fine when run in idle..
Code: Select all
pygame.mixer.init(frequency=44100, size=16, channels=2, buffer=512)
Full code as follows:
Crontab just runs a simple script on boot which in turn runs the python program after a chmod so getting it right seems to be the key.
Code: Select all
#!/usr/bin/env python3 # Pi GPIO Music player Single file # By Andrew # 1st September 2016 # Version 1.0 # # ____________________________Notes_________________________________ # 1 gpi pin triggers playback of one file in a single instance # pressing another gpi pin to ground stops playback # player waits for pin 1 to go low and will only trigger once until # completion. # __________________________________________________________________ # # Imports # from gpiozero import Button from signal import pause import pygame.mixer pygame.mixer.pre_init(44100, 16, 2, 512) from pygame.mixer import Sound global busyCh # Setup def setup(): pygame.mixer.init() global button global button2 button = Button(2)# Define button 1 button2 = Button(3) # Define button 2 busyCh = 0 pygame.mixer.music.load('files/1.wav') #load the file pygame.mixer.music.set_volume(0.4) # Set the volume. 1.0 distorts # Loop def loop(): # print("main loop") button.when_pressed = onPress #pygame.mixer.music.play def onPress(): if busyCh==0: print("playing file") pygame.mixer.music.play() # Main setup() while True: busyCh = pygame.mixer.music.get_busy() # print(busyCh) button2.when_pressed = pygame.mixer.music.stop if busyCh==0: loop()
I also have a related issue of which the above is a step in the process of an attempted fix. I did get the above to run by removing the parameters of mixer.init which I assume is putting in a lengthy buffer. This produces a working program but every few button presses, the file doesn't play and it also takes a bit of time for the file to start playing. The file needs to play almost immediately after the button press as it will be tied in with other automation from a separate system.
Many thanks for any tips / advice. I'm quite new to python so if there's any better methods, I'm very open to suggestion.
All my project needs to do is play a file once on its own and wait for another button press to make it go again.
All the best.