将大 Space 栏按钮与 Raspberry Pi 连接
Interfacing Big Space Bar Button with Raspberry Pi
我正在做一个项目,我必须依次播放 5 首 mp3 歌曲。每首歌曲的持续时间为 10 秒。为此,我正在使用 pygame 库。
一切正常,但我必须为将来在此应用程序中添加一个,我想在按下通过 Raspberry Pi 的串行端口之一连接的 BIG 按钮时开始和暂停这些 mp3 歌曲(它基本上是一个 space 栏按钮)。我希望程序在按下 space 栏按钮时继续 运行(不要通过将 space 作为中断来停止)
我是初学者,不知道该怎么做。
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
我不熟悉 Raspberry Pi,但我可以向您展示如何在 "normal" pygame 程序中执行此操作。
将您的音乐文件的名称放入列表中,并借助 set_endevent
函数和自定义事件类型以及索引变量来切换歌曲。
要暂停播放,我会定义一个 paused
布尔变量并在用户按下 Space paused = not paused
时切换它。然后以这种方式暂停和取消暂停音乐:
if paused:
pg.mixer.music.pause()
else:
pg.mixer.music.unpause()
这是一个完整的例子:
import random
import pygame as pg
pg.mixer.pre_init(44100, -16, 2, 2048)
pg.init()
screen = pg.display.set_mode((640, 480))
SONGS = ['song1.wav', 'song2.wav', 'song3.wav']
# Here we create a custom event type (it's just an int).
SONG_END = pg.USEREVENT
# When a song is finished, pygame will add the
# SONG_END event to the event queue.
pg.mixer.music.set_endevent(SONG_END)
# Load and play the first song.
pg.mixer.music.load(SONGS[0])
pg.mixer.music.play(0)
def main():
clock = pg.time.Clock()
song_idx = 0
paused = False
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
if event.type == pg.KEYDOWN:
# Press right arrow key to increment the
# song index. Modulo is needed to keep
# the index in the correct range.
if event.key == pg.K_RIGHT:
print('Next song.')
song_idx += 1
song_idx %= len(SONGS)
pg.mixer.music.load(SONGS[song_idx])
pg.mixer.music.play(0)
elif event.key == pg.K_SPACE:
# Toggle the paused variable.
paused = not paused
if paused: # Pause the music.
pg.mixer.music.pause()
else: # Unpause the music.
pg.mixer.music.unpause()
# When a song ends the SONG_END event is emitted.
# I just pick a random song and play it here.
if event.type == SONG_END:
print('The song has ended. Playing random song.')
pg.mixer.music.load(random.choice(SONGS))
pg.mixer.music.play(0)
screen.fill((30, 60, 80))
pg.display.flip()
clock.tick(30)
if __name__ == '__main__':
main()
pg.quit()
我正在做一个项目,我必须依次播放 5 首 mp3 歌曲。每首歌曲的持续时间为 10 秒。为此,我正在使用 pygame 库。
一切正常,但我必须为将来在此应用程序中添加一个,我想在按下通过 Raspberry Pi 的串行端口之一连接的 BIG 按钮时开始和暂停这些 mp3 歌曲(它基本上是一个 space 栏按钮)。我希望程序在按下 space 栏按钮时继续 运行(不要通过将 space 作为中断来停止)
我是初学者,不知道该怎么做。
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
我不熟悉 Raspberry Pi,但我可以向您展示如何在 "normal" pygame 程序中执行此操作。
将您的音乐文件的名称放入列表中,并借助 set_endevent
函数和自定义事件类型以及索引变量来切换歌曲。
要暂停播放,我会定义一个 paused
布尔变量并在用户按下 Space paused = not paused
时切换它。然后以这种方式暂停和取消暂停音乐:
if paused:
pg.mixer.music.pause()
else:
pg.mixer.music.unpause()
这是一个完整的例子:
import random
import pygame as pg
pg.mixer.pre_init(44100, -16, 2, 2048)
pg.init()
screen = pg.display.set_mode((640, 480))
SONGS = ['song1.wav', 'song2.wav', 'song3.wav']
# Here we create a custom event type (it's just an int).
SONG_END = pg.USEREVENT
# When a song is finished, pygame will add the
# SONG_END event to the event queue.
pg.mixer.music.set_endevent(SONG_END)
# Load and play the first song.
pg.mixer.music.load(SONGS[0])
pg.mixer.music.play(0)
def main():
clock = pg.time.Clock()
song_idx = 0
paused = False
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
if event.type == pg.KEYDOWN:
# Press right arrow key to increment the
# song index. Modulo is needed to keep
# the index in the correct range.
if event.key == pg.K_RIGHT:
print('Next song.')
song_idx += 1
song_idx %= len(SONGS)
pg.mixer.music.load(SONGS[song_idx])
pg.mixer.music.play(0)
elif event.key == pg.K_SPACE:
# Toggle the paused variable.
paused = not paused
if paused: # Pause the music.
pg.mixer.music.pause()
else: # Unpause the music.
pg.mixer.music.unpause()
# When a song ends the SONG_END event is emitted.
# I just pick a random song and play it here.
if event.type == SONG_END:
print('The song has ended. Playing random song.')
pg.mixer.music.load(random.choice(SONGS))
pg.mixer.music.play(0)
screen.fill((30, 60, 80))
pg.display.flip()
clock.tick(30)
if __name__ == '__main__':
main()
pg.quit()