I want Raspberry Pi to automatically turn on the LED and play the WAV file at startup.
/etc/rc.local
will stop until the LED lights up.
import pygame
import time
import RPi.GPIO as GPIO
GPIO.setmode (GPIO.BCM)
GPIO.setup(2, GPIO.OUT)
GPIO.output(2, True)
pygame.mixer.init()
pygame.mixer.music.load(sample.wav)
pygame.mixer.music.play(1)
time.sleep(10)
Try specifying the wave file with the absolute path as follows:
pygame.mixer.music.load("/path/to/sample.wav")
If you simply specify a filename, the wav file must be in the same directory as the script you ran, or it will not work. It is not recommended that you place it in /etc/rc.local
, so specify it in any directory.
© 2024 OneMinuteCode. All rights reserved.