pygame's mixer module

suggest change

The pygame.mixer module helps control the music used in pygame programs. The module functions are the 13 calls listed on the pygame page. Sound and Channel are classes.

Initializing

Similar to how you have to initialize pygame with pygame.init(), you must initialize pygame.mixer as well.

By using the first option, we initialize the module using the default values. You can override these default options. By using the second option, we can initialize the module using the values we manually put in ourselves. Standard values for pygame 1.9:

pygame.mixer.init(frequency=22050, size=-16, channels=2, buffer=4096)

Pygame 2.0 changed those defaults to frequency=44100 and buffer=512. To check whether we have initialized it or not, we can use pygame.mixer.get_init(), which returns the playback arguments (frequency, format, channels) when the mixer is initialized and None when it is not. To quit/undo the initializing, simply use pygame.mixer.quit(). If you want to continue playing sounds with the module, you might have to reinitialize the module.

Possible Actions

As your sound is playing, you can pause it temporarily with pygame.mixer.pause(). To resume playing your sounds, simply use pygame.mixer.unpause(). You can also fadeout the end of the sound by using pygame.mixer.fadeout(). It takes an argument, which is the number of milliseconds it takes to finish fading out the music.

Channels

You can play as many songs as needed as long as there are enough open channels to support them. By default, there are 8 channels. To change the number of channels there are, use pygame.mixer.set_num_channels(). The argument is a non-negative integer. If the number of channels is decreased, any sounds playing on the removed channels will immediately stop.

To find how many playback channels exist, call pygame.mixer.get_num_channels(). It takes no argument and returns that total, which is 8 by default. You can also reserve channels for sounds that must be played by using pygame.mixer.set_reserved(count). The argument is also a non-negative integer. Any sounds playing on the newly reserved channels will not be stopped.

You can also find out which channel isn’t being used by using pygame.mixer.find_channel(force). Its argument is a bool: either True or False. If there are no channels that are idle and force is False, it will return None. If force is true, it will return the channel that has been playing for the longest time.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents