将大空格键按钮与Raspberry Pi连接

时间:2018-10-03 07:48:42

标签: python pygame raspberry-pi3

我正在一个项目中,我必须依次播放5首mp3歌曲。每首歌曲的持续时间为10秒。为此,我正在使用pygame库。

一切正常,但是我必须在此应用程序中添加一个,以便将来在按通过Raspberry Pi串行端口之一连接的BIG Button时启动和暂停这些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

1 个答案:

答案 0 :(得分:2)

我不熟悉Raspberry Pi,但是我可以向您展示如何在“正常”的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()