[英]Pygame : force playing next song in queue even if actual song isn't finished? (aka "Next" button)
我想用 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.