python - Pygame - 声音延迟

标签 python pygame

我制作了一个按钮类,用于检查按钮是否被选中(当鼠标悬停在按钮上时)。当按钮被选中、取消选中或单击时,它会播放一个 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.init() 之前调用 pygame.mixer.pre_init() 解决了我的问题:

pygame.mixer.pre_init(44100, -16, 1, 512)
pygame.init()

关于python - Pygame - 声音延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18273722/

相关文章:

python - 如何获取python pandas数据框中出现的两列的唯一组合数

python - python的 "re.compile"有什么作用?

python - Surface.scroll() 是在 Pygame 中实现可移动 2D 视点的正确方法吗?

python - 是否可以使用 jedi-vim 插入 import 语句?

Python Selenium PhantomJS quit() 错误

python - 如何在Python中使用逻辑运算符

python - pygame 中的 surface.blit() 函数是什么?它有什么作用?它是如何工作的?

python - 为什么 pygame.draw.shape 返回一个 Rect?

python - 算法题: Finding the cheapest flight

python - 只有 1 个事件键有效