0

I have zero coding experience, but my son and I are trying to tackle a headless, Pi-based bluetooth soundboard to play his Little League team's walkup songs from the dugout. We've used pygame music and have gotten the gpio buttons to play the songs for a specific amount of time, and then fadeout. However, we also need a stop or "abort" button in case we need to interrupt and stop the music before its done playing. The problem is, I'm using time.sleep to set playtime, and I can't interrupt .sleep with a button wired to a .stop or .fadeout call. Time.wait doesn't seem to work either. I'm not sure if I need to learn threading, or asyncro, or something else, so looking for some help. Below is the code I've come up with for the stop button and a sample batter's button.

def Batter1():
    if pygame.mixer.music.get_busy() == True:
        pass
    else:
        pygame.mixer.music.load(BatterSong1)
        pygame.mixer.music.play(0,6.2)
        time.sleep(20)
        pygame.mixer.music.fadeout(3000)

def StopSong():
    pygame.mixer.music.fadeout(1000)

Appreciate any help!

makerdad
  • 11
  • 1
  • Instead of one sleep for 20 do a loop, i. E. 20 rotations, sleep for 1, check buttons, break loop if pressed or keep looping if not – Marcin Orlowski Apr 25 '22 at 04:39
  • Thanks for the quick reply! Sorry for the newbie question, but wouldn't the button have to be pressed at the exact millisecond when the loop isn't sleeping? Otherwise the input or function is blocked? – makerdad Apr 25 '22 at 06:32
  • That's correct. But the solution is also simple. You just increase the number of loop rotations and shorten the sleep time. That way total delay is the same but your code pulls the button state more often and therefore it will detect the desired state quicker. The shorter the sleep time the more frequent your loop rotates and the more often you are able to pull the state of the button and react to it. Adjust sleep time to your liking (you can even sleep for short time as millis if you want) and you should be good. Of course this is not ideal approach but it's just fine for your application. – Marcin Orlowski Apr 25 '22 at 07:36

0 Answers0