我创建了以下使用pygame.mixer播放mp3音乐的代码。但是,音乐不会重复。有什么想法,我会怎么做,以便音乐重复?这是代码:
playlist = list()
playlist.append ( "put music here.mp3" )
playlist.append ( "put music here.mp3" )
pygame.mixer.music.load ( playlist.pop() )
pygame.mixer.music.queue ( playlist.pop() )
pygame.mixer.music.set_endevent ( pygame.USEREVENT )
pygame.mixer.music.play()
a = 0
running = True
while a == 0:
while running:
for event in pygame.event.get():
if event.type == pygame.USEREVENT:
if len ( playlist ) >1:
pygame.mixer.music.queue ( playlist.pop() )`
答案 0 :(得分:4)
根据pygame documentation,您可以-1
传递pygame.mixer.music.play()
无限期重复音乐。
答案 1 :(得分:1)
pygame.mixer.music.play(loops=-1)
给我错误:
pygame.mixer.music.play(loops=-1)
TypeError: play() takes no keyword arguments
所做的工作只是传递数字:
pygame.mixer.music.play(-1)
希望它对某人有帮助!
答案 2 :(得分:0)
要播放音乐X
次,请使用pygame.mixer.music.play(X)
,即:
import pygame
pygame.init()
pygame.display.set_mode(pygame.display.list_modes()[-1]) # smallest resolution available
pygame.mixer.init()
pygame.mixer.music.load("test.wav")
pygame.mixer.music.play(5) # repeat 5 times
pygame.mixer.music.queue("test2.wav") # queue test2.wav after test.wav plays 5 times
clock = pygame.time.Clock()
clock.tick(10)
while pygame.mixer.music.get_busy():
pygame.event.poll()
clock.tick(10)
PS:
pygame
后,我只能用display
播放音乐。