我想用pygame制作与随身听相同的功能:播放,暂停,排队都可以。但是如何做上一个/下一个按钮?
我如何使用pygame强制播放已排队的下一首歌曲(并通过实际正在播放的ong?
有一个歌曲标题列表,并使用变量跟踪您在列表中的位置。无论您使用的是pygame.mixer.music
还是pygame.mixer.Sound
,当单击"下一步"按钮时,只需将变量更改一个,然后停止歌曲,并让变量对应的歌曲播放即可。
pygame.mixer.Sound
的代码示例:
#setup pygame above this
#load sounds
sound1 = pygame.mixer.Sound("soundone.ogg")
sound2 = pygame.mixer.Sound("soundtwo.ogg")
queue = [sound1, sound2] #note that the list holds the sounds, not strings
var = 0
sound1.play()
while 1:
if next(): #whatever the next button trigger is
queue[var].stop() # stop current song
if var == len(queue - 1): # if it's the last song
var = 0 # set the var to represent the first song
else:
var += 1 # else, next song
queue[var].play() # play the song var corresponds to
这是我为自己工作的一个例子,以围绕程序的行为进行思考。
from os import environ
environ['PYGAME_HIDE_SUPPORT_PROMPT'] = '1' # noqa This is used to hide the default pygame import print statement.
import pygame
import time
# Credit: https://forums.raspberrypi.com/viewtopic.php?t=102008
pygame.mixer.init()
pygame.display.init()
screen = pygame.display.set_mode((420, 240)) # Shows PyGame Window.
playlist = list()
playlist.append('/Music/Tom MacDonald - Angels (Explicit).mp3')
playlist.append('/Music/Falling In Reverse - Bad Girls Club (Explicit).mp3')
pygame.mixer.music.load(playlist.pop()) # Get the first track from the playlist
pygame.mixer.music.queue(playlist.pop()) # Queue the 2nd song
pygame.mixer.music.set_endevent(pygame.USEREVENT) # Setup the end track event
pygame.mixer.music.play() # Play the music
running = True
while running:
time.sleep(.1)
for event in pygame.event.get():
if event.type == pygame.USEREVENT: # A track has ended
if len(playlist) > 0: # If there are more tracks in the queue...
pygame.mixer.music.queue(playlist.pop()) # Queue the next one in the list
elif event.type == pygame.QUIT: # Create a way to exit the program.
running = False