python - Pygame - Sound delay -
i've made button class checks if button selected (when mouse hovering on button). when button selected, unselected or clicked plays wav file. problem there huge delay between sound playing , button's state changing. program should check every frame see if conditions sound play have been met fps doesn't seem problem (60 , 600 fps give same delay). i've tried decreasing buffer value in pygame.mixer.init() shows no difference.
sound files:
buttonsoundselect = pygame.mixer.sound(os.path.join(soundpath, "button1.wav")) buttonsoundunselect = pygame.mixer.sound(os.path.join(soundpath, "button2.wav")) buttonsoundclick = pygame.mixer.sound(os.path.join(soundpath, "button3.wav")) buttonsounds = [buttonsoundselect, buttonsoundunselect, buttonsoundclick] creating object:
playbutton = button(textinactive = "play", font = mainfont, sounds = buttonsounds, command = playaction) code button class checks if button selected (this inside method .display called every frame):
if pygame.mouse.get_pos()[0] >= self.x , pygame.mouse.get_pos()[0] <= self.x + self.width , \ pygame.mouse.get_pos()[1] >= self.y , pygame.mouse.get_pos()[1] <= self.y + self.height: self.surfaceactive.blit(self.textsurfaceactive, (self.width / 2 - self.font.size(self.textactive)[0] / 2, self.height / 2 - self.font.size(self.textactive)[1] / 2)) self.surface.blit(self.surfaceactive, (self.x, self.y)) if self.selected == false: if self.sounds != none: self.sounds[0].stop() self.sounds[1].stop() self.sounds[2].stop() self.sounds[0].play() self.selected = true else: self.surfaceinactive.blit(self.textsurfaceinactive, (self.width / 2 - self.font.size(self.textinactive)[0] / 2, self.height / 2 - self.font.size(self.textinactive)[1] / 2)) self.surface.blit(self.surfaceinactive, (self.x, self.y)) if self.selected == true: if self.sounds != none: self.sounds[0].stop() self.sounds[1].stop() self.sounds[2].stop() self.sounds[1].play() self.selected = false code button class checks if button clicked (this inside method .clickevent called when left mouse button clicked):
if self.command != none: if pygame.mouse.get_pos()[0] >= self.x , pygame.mouse.get_pos()[0] <= self.x + self.width , \ pygame.mouse.get_pos()[1] >= self.y , pygame.mouse.get_pos()[1] <= self.y + self.height: if self.sounds != none: self.sounds[2].play() self.command() so question is: why there long delay , can make shorter?
i had problems sound lagging. found calling pygame.mixer.pre_init() before pygame.init() solved problems:
pygame.mixer.pre_init(44100, -16, 1, 512) pygame.init()
Comments
Post a Comment