问题
I have created the following code that plays mp3 music using pygame.mixer. However, the music does not repeat. Any idea's on how I would make it so that the music does repeat? Here is the code:
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() )`
回答1:
According to the pygame documentation you can pass in -1 for pygame.mixer.music.play() to repeat the music indefinitely.
回答2:
pygame.mixer.music.play(loops=-1)
gave me error:
pygame.mixer.music.play(loops=-1)
TypeError: play() takes no keyword arguments
What did work is passing just the number:
pygame.mixer.music.play(-1)
Hope it helps someone!
回答3:
To play a music X times use, pygame.mixer.music.play(X), i.e.:
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:
- I was able to play music with
pygameonly after creating adisplay.
来源:https://stackoverflow.com/questions/35068209/how-do-i-repeat-music-using-pygame-mixer