问题是我不能连续播放 2 个声音即使使用 music.queue它只播放 1 个音乐并且程序停止,知道吗?

问题描述

我正在尝试制作一个连续播放音乐列表的程序,直到我按下按钮停止播放,为此我想使用 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() 没有阻塞

所以程序在处理队列中的第二个项目之前完成并退出。

试试:

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 以在队列结束时调用>

你想要的是一个不管队列状态如何都继续运行的循环,所以:

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()