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


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)

sounda.play() returns an object which is necessary for playing the sound. With it you can also find out if the sound is still playing:

channela = sounda.play()
while channela.get_busy():
   pygame.time.delay(100)

I had no sound from playing mixer.Sound, but it started to work after i created the window, this is a minimal example, just change your filename, run and press UP key to play:

WAVFILE = 'tom14.wav'
import pygame
from pygame import *
import sys

mixer.pre_init(frequency=44100, size=-16, channels=2, buffer=4096)
pygame.init()
print pygame.mixer.get_init() 
screen=pygame.display.set_mode((400,400),0,32) 

while True:
    for event in pygame.event.get():
        if event.type == QUIT:                                                    
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN:
            if event.key==K_ESCAPE:
                 pygame.quit()
                 sys.exit()
            elif event.key==K_UP:
                s = pygame.mixer.Sound(WAVFILE)
                ch = s.play()
                while ch.get_busy():
                    pygame.time.delay(100)
    pygame.display.update()

What you need to do is something like this:

 import pygame
 import time

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

 sounda.play()
 time.sleep (20)

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.


import pygame, time


pygame.mixer.init()
pygame.init()

sounda= pygame.mixer.Sound("beep.wav")

sounda.play()

pygame.init() goes after mixer.init(). It worked for me.