gpt4 book ai didi

python - 将大空格键与 Raspberry Pi 连接

转载 作者:行者123 更新时间:2023-12-01 01:33:13 25 4
gpt4 key购买 nike

我正在开发一个项目,其中我必须依次播放 5 首 mp3 歌曲。每首歌曲时长为10秒。为此,我使用 pygame 库。

一切工作正常,但我必须在这个应用程序中为将来添加一个,即我想在按下通过 Raspberry Pi 串行端口之一连接的大按钮时启动和暂停这些 mp3 歌曲(它基本上是一个空格键)按钮)。我希望程序在按下空格键时继续运行(而不是通过占用空格作为中断来停止)

我是初学者,不知道该怎么做。 enter image description here

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,但我可以向您展示如何在“正常”pygame 程序中执行此操作。

将音乐文件的名称放入列表中,并在set_endevent的帮助下切换歌曲。函数以及自定义事件类型和索引变量。

要暂停播放,我定义了一个 paused bool 变量,并在用户按 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()

关于python - 将大空格键与 Raspberry Pi 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52622220/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com