简体   繁体   中英

How to play only one music file in pygame?

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!

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, eg:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM