我做了一个按钮类,检查是否选中了一个按钮(当鼠标悬停在按钮上时).当选择,未选中或单击按钮时,它将播放一个wav文件.问题是声音播放和按钮状态变化之间存在巨大延迟.该程序应检查每一帧,看看是否已满足播放声音的条件,但fps似乎不是问题(60和600 fps给出相同的延迟).我试过减少pygame.mixer.init()中的缓冲区值,但这也没有显示出差异.
声音文件:
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]
创建对象:
playButton = button(textInactive = "Play", font = mainFont, sounds = buttonSounds, command = playAction)
来自按钮类的代码,用于检查是否选择了按钮(这是在每帧调用的方法.display中):
if pygame.mouse.get_pos()[0] >= self.x and pygame.mouse.get_pos()[0] <= self.x + self.width and \
pygame.mouse.get_pos()[1] >= self.y and 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
来自按钮类的代码,用于检查是否单击了按钮(这是在单击鼠标左键时调用的方法.clickEvent中):
if self.command != None:
if pygame.mouse.get_pos()[0] >= self.x and pygame.mouse.get_pos()[0] <= self.x + self.width and \
pygame.mouse.get_pos()[1] >= self.y and pygame.mouse.get_pos()[1] <= self.y + self.height:
if self.sounds != None:
self.sounds[2].play()
self.command()
所以我的问题是:
为什么会有很长的延迟,我可以缩短它吗?
在使用Pygame制作的程序中,作者遇到了声音播放与按钮状态改变之间存在显著延迟的问题。即使调整fps或减少缓冲区大小,延迟仍然存在。按钮类检查鼠标位置来改变其状态并播放相应的wav文件。当鼠标悬停、选中或点击按钮时,会播放不同的声音。作者寻求减少这种延迟的方法。
338

被折叠的 条评论
为什么被折叠?



