No sound in Pygame

Silence, “mixer not initialized” or delayed sounds usually come from the audio device, the file format or the order of initialisation. Check these in turn.

2 min readUpdated
On this page

Quick checks#

  1. Is your system volume up, and does sound play in other programs?
  2. Did pygame.init() (or pygame.mixer.init()) run before you created Sound objects?
  3. Print pygame.mixer.get_init(). None means the mixer isn't running.
  4. Are you keeping a reference to the Sound? Creating and playing a sound in one line works, but a program that exits immediately after play() ends before you hear anything.

"pygame.error: mixer not initialized"#

The mixer couldn't open an audio device. Common situations:

  • No audio hardware: servers, some virtual machines, containers and CI systems.
  • WSL: older WSL setups have no audio output; WSL2 with WSLg on Windows 11 can play sound, depending on configuration.
  • Device busy or misconfigured on Linux: check that PulseAudio or PipeWire is running and other apps can play sound.

Make your game survive without sound:

Python
try:
    pygame.mixer.init()
    jump = pygame.mixer.Sound("jump.ogg")
except pygame.error:
    jump = None            # play nothing, but keep the game running

if jump:
    jump.play()

You can also choose an SDL audio driver by setting the SDL_AUDIODRIVER environment variable before starting Python (for example pulseaudio, pipewire, alsa or directsound, depending on your system).

The file doesn't play#

  • Format: use OGG Vorbis or 16-bit PCM WAV. Some WAV variants (24-bit, ADPCM, unusual sample rates) can fail with Unrecognized audio format. Re-export the file with an audio editor such as Audacity.
  • Path: build paths from your script's folder, Path(__file__).parent / "sounds" / "jump.ogg", as explained in images and sprites.
  • Music vs effects: pygame.mixer.music.load() must be followed by pygame.mixer.music.play(). Loading alone is silent.
  • All channels busy: with many effects, sound.play() returns None. Increase channels with pygame.mixer.set_num_channels(16).

Delayed or crackling sound#

A large audio buffer delays sounds; a very small one crackles. Set it before pygame.init():

Python
pygame.mixer.pre_init(44100, -16, 2, 512)
pygame.init()

Try 512, 1024 or 2048 until sound is both immediate and clean. Also make sure your sounds' sample rate matches the mixer's (44100 Hz is typical).

Music doesn't loop#

Pass loops=-1 to play forever: pygame.mixer.music.play(loops=-1). Without it the track plays once.

In the browser#

Browsers block audio until the visitor interacts with the page, and audio support in WebAssembly builds can be limited. Sound in Pygame Online may be silent; run the game locally to test audio.

↑ ↓ to moveEnter to openEsc to close