问题是我不能连续播放两个声音(即使使用music.queue),它只播放一个音乐,节目就停止了,你知道吗?

2024-05-14 20:58:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图制作一个程序,连续播放一列音乐,直到我按下按钮停止播放,为此我想使用pygame.mixer.music。 问题是我不能连续播放2种声音(即使使用music.queue),它只播放1种音乐,并且程序停止: 我试过:

pygame.mixer.music.load("music1")
pygame.mixer.music.queue("music2")
pygame.mixer.music.play()

但什么都没用


Tags: 程序声音playqueue音乐musicload按钮
1条回答
网友
1楼 · 发布于 2024-05-14 20:58:11

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

相关问题 更多 >

    热门问题