The Python Game Book

code games. learn Python.

User Tools

Site Tools




This is an old revision of the document!

Step 010 - Using Sound and Music

Code Discussion

Pygame has several commands to play sound. Pygame offers a set of Sound commands to load and play short sound files - usually .wav or .ogg files. For longer files, pygame offers commands to stream longer music / sound / voice files directly from the harddisk.

In the source code example below, loading sounds and music is done from a subfolder called data, like the graphic files in the previous source code example.

Initializing the Mixer

To get rid of a nasty delay between giving the play command for a sound and hearing it you need to initialize the pygame mixer. This is done before writing the pygame.init() command. Under Linux, I got best results with this line:

pygame.mixer.pre_init(44100, -16, 2, 2048) # setup mixer to avoid sound lag
pygame.init()                      #initialize pygame

loading music and sound files

Note that loading the sound and music files from harddisk must be done after setting up the mixer. In the code-example below, an-turr.ogg is a longer music file while jump.wav and fail.wav are short sound effects:

try:'data', 'an-turr.ogg'))#load music
    jump = pygame.mixer.Sound(os.path.join('data','jump.wav'))  #load sound
    fail = pygame.mixer.Sound(os.path.join('data','fail.wav'))  #load sound
    raise UserWarning, "could not load or play soundfiles in 'data' folder :-("

Playing Music and Sound from Files

After loading the sound effects or the music and thus creating pygame objects, you can simply call the .play() method of those objects. Inside the brackets you write -1 for endless play or the number of times or the time length to play the sound.

#for sound effects:, maxtime=0, fade_ms=0): return Channel # play the jump sound effect once
The music is usually played endlessly:
#music is already the name of the music object, start=0.0): return None # play endless

<note tip>you hear just a short crack noise instead of a sound? Some sounds simply do not work with pygame. Try to open (and edit) the sound with a sound editor like Audacity and save it again, preferable in the free .ogg (vorbis) format.</note>

versions with and without graphic

Note that you do not need pygame's graphic at all to play music files. One of the code examples below use graphical output, the other example use only text output. In text output, you still need to initialize pygame and set up the mixer.

Generate Sound Effects

Unlike music, sound effects are easy enough to generate and not worth the hassle of copyright fights. You may find good sound effects here:

To generate sound effects, you can either:


Source Code on Github

To run this example you need:

file in folder download pygame Download the whole Archive with all files from Github: pygame
fail.wav pygame/data
jum.wav pygame/data

without graphic

View/Edit/Download the file directly in Github:

click reload in your browser if you see no code here:

with graphic

View/Edit/Download the file directly in Github:

click reload in your browser if you see no code here:

Comment this Page


if you have none at hand, use your headphone (sic!). Most headphones can serve as an (low-quality) microphone.
en/pygame/step010.1389262056.txt.gz · Last modified: 2014/01/09 11:07 by