pygame.mixer plays two kinds of audio:
- Sounds (
pygame.mixer.Sound): short effects loaded into memory, such as a jump or a coin. Many can play at once. - Music (
pygame.mixer.music): one long track streamed from its file, usually looping in the background.
Press Play, click the game, and press Space, the arrow keys or the mouse button to hear the effects. The music starts with the game.
The sounds and music are from the free pack in the assets folder (Kenney, CC0). On your computer, use your own files and paths.
Sound effects
laser = pygame.mixer.Sound("laser.ogg") # load once, before the loop
laser.play() # each time you need it
| Method | Does |
|---|---|
sound.play() | Plays it once; returns the channel it plays on |
sound.play(loops=2) | Plays it three times (once plus two repeats); -1 loops forever |
sound.play(maxtime=500) | Stops after 500 milliseconds |
sound.set_volume(0.5) | Volume from 0.0 to 1.0 |
sound.stop() | Stops every copy of it that is playing |
sound.fadeout(300) | Fades out over 300 milliseconds |
sound.get_length() | Its length in seconds |
Load each file once. Calling pygame.mixer.Sound("laser.ogg") inside the loop reads the file from disk on every shot.
Background music
pygame.mixer.music.load("theme.ogg")
pygame.mixer.music.play(-1) # -1 repeats forever
pygame.mixer.music.set_volume(0.4)
| Function | Does |
|---|---|
music.load(path) | Picks the track (only one at a time) |
music.play(loops=0, start=0.0, fade_ms=0) | Starts it; loops=-1 repeats, fade_ms fades in |
music.stop() | Stops it |
music.pause(), music.unpause() | Pause and resume, for a pause menu |
music.fadeout(1000) | Fades out over a second |
music.set_volume(v) | Volume from 0.0 to 1.0 |
music.get_busy() | True while music is playing |
To change tracks, for example from the menu theme to the level theme, call load() and play() again.
Formats
| Format | Use |
|---|---|
| WAV | Effects. No compression, loads fast, larger files |
| OGG | Effects and music. Small and works everywhere |
| MP3 | Music, in recent pygame and pygame-ce |
OGG is the safest single choice. A wrong path raises FileNotFoundError: No file 'jump.ogg' found in working directory ..., the same message as for images; a file pygame cannot decode raises a pygame.error.
Channels
Each playing sound uses a channel; pygame has 8 by default. When all are busy a new sound may not play. pygame.mixer.set_num_channels(16) allows more, and pygame.mixer.Channel(0).play(sound) reserves a channel for something that must always be heard, such as the player's voice.
Frequently Asked Questions
How do I play a sound in pygame?
Load it once, jump = pygame.mixer.Sound("jump.ogg"), and call jump.play() when the event happens. Several sounds can play at the same time.
How do I loop background music?
pygame.mixer.music.load("theme.ogg") then pygame.mixer.music.play(-1). -1 repeats forever. Music streams from the file instead of loading it all, which suits long tracks.
What sound formats does pygame support?
WAV and OGG work everywhere and are the safest choices; recent pygame and pygame-ce also play MP3. Use short WAV or OGG files for effects and OGG for music.
Why do I get pygame.error: mixer not initialized?
The mixer did not start, either because pygame.init() was not called or because the computer has no working audio device. Call pygame.mixer.init() and check pygame.mixer.get_init().