2
votes

I am trying to play simple wav files in my little game whenever the user hovers over a button, a little click sound is played (same goes for when the button is actually pressed). With that I encounter the generic lag that everyone who has not initialized the mixer properly does. However, when I correctly initialize it, it still has a .5 second delay. I initialize my game as follows:

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

I have fiddled around (is that a word?) with the different init settings, without success. The wav file is clear and the audio starts immediately from the beginning of the file. So my question is: are there any other ways to remedy the lag that occurs when playing sounds in pygame?

2
Are you sure this is causing the problem, because when I used sound in my breakout game there was no lag. This is the code I used pygame.mixer.pre_init(44100, -16, 2, 2048) # setup mixer to avoid sound lag pygame.init() #initialize pygame pygame.mixer.init(44100, -16, 2, 2048) - marienbad
No I am not, I just tested it out in a different file, isolating the sound playback, and it worked there. I guess I initialized stuff at the wrong place. Thanks anyways for your answer! - Gouatsie

2 Answers

4
votes

THE ANSWER:

I wound up initiating the mixer with pre_init settings, then quitting the mixer, then initiating it again:

pygame.mixer.pre_init(22050, -16, 2, 1024)
pygame.init()
pygame.mixer.quit()
pygame.mixer.init(22050, -16, 2, 1024)

Now it works just fine.

1
votes

In my case I found that it's sufficient to just quit the mixer before it's started (even if it's never been started before):

pygame.mixer.quit()
pygame.mixer.init(44100, -16, 2, 1024)