A couple of days back, my kids were watching The Voice.
You probably know it: It’s a singing contest, but the jury can’t see the singers. When they like the song, they press their button to turn around.
My kids were playing jury: They sat backwards in the couch, and only turned when they liked the song.
I liked the idea, but I wanted to give them a real button to press. So I’ve recycled some components from my Timer Button and Wicked Witch project, and used them to build a working Voice button.
Now it’s even more realistic!
And the coding for it is actually quite simple:
from gpiozero import Button
import time
import pygame
#initialize button
button = Button(12)
#initialize sound
pygame.mixer.pre_init(frequency=44100, size=-16, channels=2, buffer=4096)
pygame.mixer.init()
pygame.mixer.music.set_volume(1.0)
#Start script: play sound when button is pressed
print ("Start script...")
while True:
button.wait_for_press()
print ("Button pressed! ")
pygame.mixer.music.load("/home/pi/Documents/Programs/TheVoice/VoiceButton.mp3")
pygame.mixer.music.play()
while pygame.mixer.music.get_busy() == True:
pass
time.sleep(0.1)