Making SoundsΒΆ

Pyglet can also be used to make sounds. Doing so is a three step process:

  1. Tell Pyglet what audio driver to use
  2. Load a sound file into a Pyglet object with
  3. Use that object’s play() method.

Here is some code that demonstrates this:

import pyglet
import sys

#Tell pyglet what driver to use
pyglet.options['audio'] = ('openal', 'silent')

class SoundWindow(pyglet.window.Window):

    def __init__(self,*args,**kwargs):
        pyglet.window.Window.__init__(self, *args,**kwargs)

    def on_draw(self):
        print 'The window was drawn!'
        print 'We are going to make a noise'

    def make_sound(self):
        source ='bicycle_bell.wav', streaming=False)

sound_window = SoundWindow()

We won’t show a screenshot of the result as they are not visual, but trust us–it works. Note that we set the keyword argument streaming=False in our invocation of This allows us to play the sound multiple times, which in most cases we will want to do. However, if you only wanted to play a sound once, you could omit this parameter.

If you want to run this code for yourself, you will have to download a .wav file and put it in the same directory as this script. The one used in the example can be found here:

You may also need to install the openal package, from

The full documentation on making sounds (and video) in Pyglet can be found here:

Next Section - A Movable Sqaure with Jingles