0

i'm currently trying to autoboot a python script via rc.local. The python script is about playing a Sound when a button is pressed:

import RPi.GPIO as GPIO
import pygame
import pygame.mixer

GPIO.setmode(GPIO.BOARD)
GPIO.setup(37, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

pygame.init()
pygame.mixer.init()

ausgeloest = True # bool for making only a sound after the button was released
while True:
     if GPIO.input(37) == 0:
           ausgeloest = False
     elif GPIO.input(37) == 1:
           #my code before: pygame.mixer.music.load("Sound1.wav")
           #my code before: pygame.mixer.music.play()
           #new code (thanks to the answers):
           music = pygame.mixer.Sound("/home/pi/soundmachine/Sound1.wav")
           music.play()
           
           print("Sound was played") #for testing purposes
           ausgeloest = True

The script works perfectly in desktop mode, when i run it manually. I've set the default audio-output via right-click on the speaker-sign to analog, because i want to use the 3.5mm output. But as soon as i switch into console-mode (auto-login) and try to run it, there is no audio output.

Update: The error message is now gone and i can see the testing print()-line on the console, but i still can't hear the desired sound... So the script is being executed, but somehow the sound is not hearable. How can I solve this problem?

  • Did you activate the environment before running from the console? Maybe if you pass the `Sound1.wav` path as an environment variable might solve your problem. For instance: `python -path="C:\\Users\\..\\..\\Sound1.wav" main.py` – Ahmet Nov 27 '20 at 22:37
  • @Ahx what do you mean? I'm kinda new to Raspberry Pi and to Python, so what do i have to do in your opinion? – JoshFrosch Nov 28 '20 at 20:43

0 Answers0