New to Python, using Raspberry Pi, GPIO and pygame. I want to end a loop if a button is pressed while playing a media file.
Right now I have it so you press the button and the mp3 file plays all the way through (it is a 30 minute clip). I would like it so if the button is pressed again, the media resets and plays back from the beginning.
I have tried adding a break and an if statement but it just ignores it because the mp3 file is already playing. How would I go about doing this?
Here is what my code looks like:
while True:
while GPIO.input(buttonPin) == GPIO.LOW:
if GPIO.input(buttonPin) == GPIO.LOW:
pygame.mixer.init()
pygame.mixer.music.load(open("audio.mp3"))
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
time.sleep(1)
else:
break