Playing sounds and music

pyglet makes it easy to play and mix multiple sounds together in your game. The following example plays an MP3 file [5]:

import pyglet

music ='music.mp3')

As with the image loading example presented earlier, locates the sound file in the application's directory (not the working directory). If you know the actual filesystem path (either relative or absolute), use

Short sounds, such as a gunfire shot used in a game, should be decoded in memory before they are used, so that they play more immediately and incur less of a CPU performance penalty. Specify streaming=False in this case:

sound ='shot.wav', streaming=False)

The examples/ example demonstrates playback of streaming audio and video using pyglet. The examples/noisy/ example demonstrates playing many short audio samples simultaneously, as in a game.

[5]MP3 and other compressed audio formats require AVbin to be installed (this is the default for the Windows and Mac OS X installers). Uncompressed WAV files can be played without AVbin.