Bge.aud playing the same audio more than once

Hi, I’m trying to use the “aud” module to play an audio. I can play the audio by pressing the space key (just activated). But I’m having a problem, when I press space, the game engine crashes for milliseconds (probably loading the audio) and when it returns, it plays again, rotating the audio twice, creating an embarrassment of audios. Is it possible to solve this?

My code:

import aud
import bge

audioPath = bge.logic.expandPath("your audio path")
audioName = "your audio name"

device = aud.device()
path_with_sound = ("{}\{}".format(audioPath.replace("\\", "\ ".replace(" ", "")), audioName))
main_sound = aud.Factory(path_with_sound)

keyboard = bge.logic.keyboard.inputs
space = keyboard[bge.events.SPACEKEY]
play_tap = space.activated

if play_tap:
    device.play(main_sound)
    factory_buffered = aud.Factory.buffer(main_sound)
    handle_buffered = device.play(factory_buffered)

Buffer the file when you first launch the game. Put the buffer in a bge.logic variable so you can access it later.

^ This is the only bit you need to run when you push space.

1 Like