Reputation:
import pygame,sys
pygame.init()
size = 40,40
screen = pygame.display.set_mode(size)
LEFT = 1
s = pygame.mixer.Sound("Sound.wav")
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == LEFT:
s.play()
So at the moment, I'm trying to make a game using Pygame. I have it set so a sound plays whenever you click the mouse. It works and all, but I get really annoyed, because I can only hear one sound effect at a time. If I click 7 times very quickly, I might hear only 4-6 sounds. If I click 2 times very quickly, I only hear one sound play.
I've looked on various other questions about overlapping Pygame sounds (it's got something to do with pygame.mixer.Channel, which I don't understand). Nothing has worked for me so far. Any ideas?
Upvotes: 1
Views: 1496
Reputation: 61
Old thread, but some one like me might be struggling with the same problem
For CodeSurgeons solution to work I had to initialize the pygame.mixer with a smaller buffer size
pygame.mixer.init(buffer=2048)
or
pygame.mixer.pre_init(buffer=2048)
pygame.init()
Upvotes: 2
Reputation: 2465
Playing around a bit, I think that you are right about using pygame.mixer.Channel
. I would recommend taking a look at the pygame.mixer
as well as the pygame.mixer.Channel
pages. The simplest approach I could come up with is to see if there is an empty channel available in pygame (8 channels are available by default, more can be created with pygame.mixer.set_num_channels()
). If there is an empty one available, then you can use the channel instead to play the sound rather than the sound object itself. With those two tweaks, your sample becomes this:
import pygame
import sys
pygame.init()
size = (200, 200)
screen = pygame.display.set_mode(size)
LEFT = 1
s = pygame.mixer.Sound("Sound.wav")
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == LEFT:
print "click"
empty_channel = pygame.mixer.find_channel()
empty_channel.play(s)
Let me know if it helps!
Upvotes: 0