Pygame - Sound delay
Simply decreasing the size of the buffer, as often suggested for this problem, did not work for me. I found this solution. When initiating the mixer twice, the delay completely disappeared:
import pygame
pygame.mixer.pre_init(22050, -16, 2, 1024)
pygame.init()
pygame.mixer.quit()
pygame.mixer.init(22050, -16, 2, 1024)
It's a bit dirty and I don't know why it works, but hopefully it solves the problem for some people.
Tested on Ubuntu 16.04 LTS with Python 3.6 and pygame 1.9.4
I know this is old, but I found the best solution I have seen so far.
The fix is quite simple actually. I used to have delay in my pygame projects all the time because I would initialize pygame before initializing the mixer. (which always seemed the way you should do it to me).
However if you initialize the mixer before initializing pygame itself it gets rid of all delay. This fixed all my delay problems. hope it helps.
pygame.mixer.pre_init(44100, -16, 2, 2048)
pygame.mixer.init()
pygame.init()
I also had problems with sound lagging. I found that calling pygame.mixer.pre_init()
before pygame.init()
solved my problems:
pygame.mixer.pre_init(44100, -16, 1, 512)
pygame.init()
Decreasing the size of the buffer will reduce the latency. The buffer must be a power of 2. The default buffer is 4096, but you can change it when you initialize mixer as shown below:
pygame.mixer.init(44100, -16, 2, 64)
More information can be found on the pygame docs