the topic name might seem a bit chaotic, but I'm struggling to describe in one line what I mean. I want to create a python script that plays different sounds when certain button is pressed. So far I came to this point:
import RPi.GPIO as GPIO import time import pygame GPIO.setmode(GPIO.BCM) GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP) pygame.mixer.init() bang = pygame.mixer.Sound('aa.wav') while True: input_state = GPIO.input(18) input_state2 = GPIO.input(17) if input_state == False: print('Button pressed') bang.play() time.sleep(1.5) if input_state2 == False: print('Button2 pressed') time.sleep(0.3) GPIO.cleanup()
I used time.sleep(1.5), because this is how long the sound is. If I don't do that, when I press the button I hear the sound multiple times (and I understand that this is how it's supposed to be). All this implies that on a button press I want to hear the sound ONCE only, and then it can play again after the previous execution is finished.
Technically the sleep solves the problem... But only when there is one sound I want to use. Is there a chance I can in some way get a polyphonic output? Like... When I press both buttons in the same time, I get two different sounds (but each of them still plays only once)? I believe the sleep function doesn't have use in this case, but as I'm new to python, I don't know an alternative and I don't know how to look for it...
Thanks for any help, S.