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
This will work on python 3
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:
What you need to do is something like this:
The reason I told the program to sleep is because I wanted a way to keep it running without typing lots of code. I had the same problem and the sound didn't play because the program closed immediately after trying to play the music.
In case you want the program to actually do something just type all the necessary code but make sure it will last long enough for the sound to fully play.
Just try:
This should work. You just need to add
print ""
and the sound will have had time to load its self.I've had something like this happen. Maybe you have the same problem? Try using an absolute path:
Where abslute_path is obviously replaced with your actual absolute path ;)
good luck.
Your code plays
desert_rustle.wav
quite fine on my machine (Mac OSX 10.5, Python 2.6.4, pygame 1.9.1). What OS and Python and pygame releases are you using? Can you hear the.wav
OK by other means (e.g.open
on a Mac's terminal orstart
on a Windows console followed by the filename/path to the.wav
file) to guarante the file is not damaged? It's hard to debug your specific problem (which is not with the code you give) without being able to reproduce it and without having all of these crucial details.