2024-05-14 20:58:11 发布
网友
我试图制作一个程序,连续播放一列音乐,直到我按下按钮停止播放,为此我想使用pygame.mixer.music。 问题是我不能连续播放2种声音(即使使用music.queue),它只播放1种音乐,并且程序停止: 我试过:
pygame.mixer.music.load("music1") pygame.mixer.music.queue("music2") pygame.mixer.music.play()
但什么都没用
pygame.mixer.music.play()没有阻塞
pygame.mixer.music.play()
因此,程序将在处理队列中的第二项之前完成并退出
尝试:
import time pygame.mixer.music.load("music1") pygame.mixer.music.queue("music2") pygame.mixer.music.play() time.sleep(60) # or however long you need to wait for the next song to play
如果要在音乐停止时退出程序,可以轮询混音器的get_busy状态,也可以注册对end_event的回调,该回调在队列结束时调用
get_busy
end_event
您需要的是一个无论队列状态如何都能继续运行的循环,因此:
import pygame pygame.init() pygame.mixer.music.load("music1.mp3") pygame.mixer.music.queue("music2.mp3") pygame.mixer.music.play() screen = pygame.display.set_mode((400,400)) clock = pygame.time.Clock() paused = False done = False while not done: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.mixer.music.stop() done = True elif event.type == pygame.KEYDOWN: #press a key to pause and unpause if paused: pygame.mixer.music.unpause() paused = False else: pygame.mixer.music.pause() paused = True clock.tick(25) pygame.quit()
pygame.mixer.music.play()
没有阻塞因此,程序将在处理队列中的第二项之前完成并退出
尝试:
如果要在音乐停止时退出程序,可以轮询混音器的
get_busy
状态,也可以注册对end_event
的回调,该回调在队列结束时调用您需要的是一个无论队列状态如何都能继续运行的循环,因此:
相关问题 更多 >
编程相关推荐