Unable to Play WAV in Raspberry Pi Background

Asked 2 years ago, Updated 2 years ago, 49 views

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)

python raspberry-pi

2022-09-30 17:49

1 Answers

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.


2022-09-30 17:49

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.