How to play only one music file in pygame?

Viewed 130

I've loaded 2 music files as a wav into my program, but when I do pygame.mixer.music.play(-1) it plays both over the top of each other, but I only want one to play, and then the other at another time.

I would imagine there would be a way to "unload" the music or something, but not sure, so any methods are greatly appreciated!

1 Answers

According to pygame docs (https://www.pygame.org/docs/ref/music.html#pygame.mixer.music.load), you can use these methods to load, unload and play musicfiles:

// Load musicfile
pygame.mixer.music.load(filename)

// Unload current musicfile
pygame.mixer.music.unload()

// Play current loaded musicfile
pygame.mixer.music.play()

Make a method that takes a musicfile as a parameter, load it and play it, e.g.:

def play_music(filename)
    // Unload the current loaded music file
    pygame.mixer.music.unload()

    pygame.mixer.music.load(filename)
    pygame.mixer.music.play()
Related