Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pygame, sounds don't play

I'm trying to play sound files (.wav) with pygame but when I start it I never hear anything.
This is the code:

import pygame  pygame.init() pygame.mixer.init() sounda= pygame.mixer.Sound("desert_rustle.wav")  sounda.play() 

I also tried using channels but the result is the same

like image 913
Gabriele Cirulli Avatar asked May 29 '10 23:05

Gabriele Cirulli


People also ask

Can pygame play MP3 files?

Pygame can load WAV, MP3, or OGG files. The difference between these audio file formats is explained at http://invpy.com/formats. To play this sound, call the Sound object's play() method. If you want to immediately stop the Sound object from playing call the stop() method.

How do I add sound effects to pygame?

Pygame provides an easy way to integrate sounds into your Python video game. Pygame's mixer module can play one or more sounds on command, and by mixing those sounds together, you can have, for instance, background music playing at the same time you hear the sounds of your hero collecting loot or jumping over enemies.


1 Answers

For me (on Windows 7, Python 2.7, PyGame 1.9) I actually have to remove the pygame.init() call to make it work or if the pygame.init() stays to create at least a screen in pygame.

My example:

import time, sys from pygame import mixer  # pygame.init() mixer.init()  sound = mixer.Sound(sys.argv[1]) sound.play()  time.sleep(5) 
like image 50
Trilarion Avatar answered Sep 22 '22 12:09

Trilarion