Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pygame error: mixer system not initialized

I've just started a little game project and I'm trying to make it play a sound everytime a bullet is fired but i keep getting the same error:

pygame.error: mixer system not initialized

I don't get what I've done wrong, so here is my code:

import pygame, sys
from pygame.locals import *

theClock = pygame.time.Clock()

sound = pygame.mixer.Sound("bullet.mp3")

....

if event.type == KEYDOWN:
    if event.key == K_SPACE and shot_count == 0:
        sound.play()
        shot_y = h-50
        shot_x = x
    elif event.type == K_SPACE and shot_count == 1:
        shot_y_2 = h-50
        shot_x_2 = x
    print(h, ' ', shot_y, shot_count)
if event.type == KEYUP:
    if event.key == K_SPACE and shot_count == 0:
        resetShot = 0 
    elif event.type == K_SPACE and shot_count == 1:
        resetShot = 0
like image 781
Fellow Rémi Avatar asked Mar 06 '14 14:03

Fellow Rémi


People also ask

How do I fix pygame error mixer is not initialized?

Simple workaround is to run the sript in the python shell or from the IDLE(pretty much the same). And it should run without any errors.

How do I stop pygame mixer?

To stop playing the background music immediately, call the pygame. mixer. music. stop() function.

How do I control the volume on pygame?

Right click on the speaker icon in the taskbar. Select Playback Devices. Select Speakers and Properties. Go to Enhancements tab and uncheck Equalizer and Loudness Equalization.


2 Answers

I was looking here and found out that pygame only loads OGG ang uncompressed WAV files. Another issue is you forgot to initialize the pygame.mixer module.

pygame.mixer.init()

is the most simple way to initialize the pygame.mixer module. For more info, go to the previous link.

like image 193
Song Avatar answered Sep 24 '22 02:09

Song


You need to pygame.init() before using mixer/sound objects.

According to documentation, you should use OGG or WAV sound files.

like image 29
pmoleri Avatar answered Sep 25 '22 02:09

pmoleri