I tried using the queue function, but
pygame.mixer.music.queue(filename)
doesn\'t seem to be working.
Here is the code I use to ru
Put your music files (paths) into a list, define a custom userevent and call pygame.mixer.music.set_endevent(YOUR_USEREVENT). Then pygame will add this event to the event queue when a song is finished and you can execute some code to change the index of the current song. In the example below you can either increment the index by pressing the right arrow key or wait until a song is finished (the SONG_FINISHED
event is emitted) and the program will choose a random song (index).
import random
import pygame as pg
pg.mixer.pre_init(44100, -16, 2, 2048)
pg.init()
screen = pg.display.set_mode((640, 480))
# A list of the music file paths.
SONGS = ['file1.ogg', 'file2.ogg', 'file3.ogg']
# Here we create a custom event type (it's just an int).
SONG_FINISHED = pg.USEREVENT + 1
# When a song is finished, pygame will add the
# SONG_FINISHED event to the event queue.
pg.mixer.music.set_endevent(SONG_FINISHED)
# Load and play the first song.
pg.mixer.music.load('file1.ogg')
pg.mixer.music.play(0)
def main():
clock = pg.time.Clock()
song_idx = 0 # The index of the current song.
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
elif event.type == pg.KEYDOWN:
# Press right arrow key to increment the
# song index. Modulo is needed to keep
# the index in the correct range.
if event.key == pg.K_RIGHT:
print('Next song.')
song_idx += 1
song_idx %= len(SONGS)
pg.mixer.music.load(SONGS[song_idx])
pg.mixer.music.play(0)
# When a song ends the SONG_FINISHED event is emitted.
# Then just pick a random song and play it.
elif event.type == SONG_FINISHED:
print('Song finished. Playing random song.')
pg.mixer.music.load(random.choice(SONGS))
pg.mixer.music.play(0)
screen.fill((30, 60, 80))
pg.display.flip()
clock.tick(30)
if __name__ == '__main__':
main()
pg.quit()