I am working on a project in which I have to play 5 mp3 songs in a sequence one after another. Each song has a duration of 10s. For that purpose, I am using pygame library.
All is working fine but I have to add one for future in this application that is I want to start and pause these mp3 songs on pressing BIG Button that is connected through one of the serial port of Raspberry Pi (It's basically a spacebar button). I want program keep on running on pressing spacebar button(not to stop by taking space as an interrupt)
I am a beginner level and don't know how to do this.
import time
import random
import RPi.GPIO as GPIO
import pygame
pygame.mixer.init()
# setting a current mode
GPIO.setmode(GPIO.BCM)
#removing the warings
GPIO.setwarnings(False)
#creating a list (array) with the number of GPIO's that we use
pins = [16,20,21]
#setting the mode for all pins so all will be switched on
GPIO.setup(pins, GPIO.OUT)
GPIO.output(16, GPIO.HIGH)
GPIO.output(20, GPIO.HIGH)
GPIO.output(21, GPIO.HIGH)
temp = ''
new = ''
def call_x():
GPIO.output(16, GPIO.LOW)
time.sleep(10)
GPIO.output(16, GPIO.HIGH)
def call_y():
GPIO.output(20, GPIO.LOW)
time.sleep(10)
GPIO.output(20, GPIO.HIGH)
def call_z():
GPIO.output(21, GPIO.LOW)
time.sleep(10)
GPIO.output(21, GPIO.HIGH)
def song(val):
if (select == 'a'):
#1............................................................
pygame.mixer.music.load("Shinsuke-Nakamura.mp3")
pygame.mixer.music.play()
time.sleep(0.2)
if(val == 'x'):
call_x()
if(val == 'y'):
call_y()
if(val == 'z'):
call_z()
temp = 'a'
return temp
if (select == 'b'):
#2............................................................
pygame.mixer.music.load("John-Cena.mp3")
pygame.mixer.music.play()
time.sleep(0.2)
if(val == 'x'):
call_x()
if(val == 'y'):
call_y()
if(val == 'z'):
call_z()
temp = 'b'
return temp
if (select == 'c'):
#3............................................................
pygame.mixer.music.load("Brock-Lesnar.mp3")
pygame.mixer.music.play()
time.sleep(0.2)
if(val == 'x'):
call_x()
if(val == 'y'):
call_y()
if(val == 'z'):
call_z()
temp = 'c'
return temp
try:
while 1:
select = random.choice('abcdefghij')
select1 = random.choice('xyz')
if (temp != select and select1 != new):
temp = song(select1)
new = select1
else:
print('repeat')
except KeyboardInterrupt:
GPIO.cleanup() # clean up GPIO on CTRL+C exit
pygame.mixer.music.stop()
pygame.mixer.music.stop()
GPIO.cleanup() # clean up GPIO on normal exit