I have also had errors with pygame.mixer.music.queue, but you can use the following class:
import pygame
class PlayList:
playlist_on_play = None
def __init__(self, list_files):
self.list_files = list_files
self.song_num = 0
self.reproducing = False
def play(self):
if PlayList.playlist_on_play is not None:
PlayList.playlist_on_play.stop()
PlayList.playlist_on_play = self
def stop(self):
self.reproducing = False
self.song_num = 0
if PlayList.playlist_on_play is self:
PlayList.playlist_on_play = None
@staticmethod
def check(event):
if PlayList.playlist_on_play is not None:
pl = PlayList.playlist_on_play
if not pl.reproducing: # play first song
pl.play_song(pl.list_files[pl.song_num])
pl.reproducing = True
if event.type == MUSIC_END and pl.song_num < len(pl.list_files)-1: # play rest of songs
pl.song_num += 1
pl.play_song(pl.list_files[pl.song_num])
if event.type == MUSIC_END and pl.song_num == len(pl.list_files)-1: # end playlist
pl.stop()
print("end playlist")
def play_song(self, file):
pygame.mixer.music.load(file)
pygame.mixer.music.play()
as follows
if __name__ == "__main__":
pygame.init()
screen = pygame.display.set_mode((400, 300))
MUSIC_END = pygame.USEREVENT+1
pygame.mixer.music.set_endevent(MUSIC_END)
list_1 = ["a2.mp3", "a2.mp3", "a2.mp3"]
list_2 = ["a3.mp3", "a3.mp3", "a3.mp3"]
playlist1 = PlayList(list_1)
playlist2 = PlayList(list_2)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
PlayList.check(event)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
print("playlist 1")
playlist1.play()
if event.key == pygame.K_w:
print("playlist 2")
playlist2.play()
if event.key == pygame.K_ESCAPE:
break
pygame.quit()
that worked for me