Playing audio files in UPBGE/BGE
Have you ever tried to make a game/application with the UPBGE/BGE and noticed some strange behavior with large audio sounds? like some audio lag.
Like then years ago I did a project wich had some large video introduction, and wheter the audio sound was in the video file or separated, we always got the same result: audio lag
At that moment we were unable to find a solution for this, but now after looking around some forums I found a solution for this and decided to post it here, just in case.
This is the script you need to use to fix audio lag:
sfx_music = aud.Factory.buffer(aud.Factory(bge.logic.expandPath(str("//Audio/yourmusicORaudio.wav")))) music = aud.device().play(sfx_music) music.volume = 0.5 music.loop_count = -1
Volume & loop_count are just 2 of very many functions that you can enable for per-sound.
If you want to find out more of them do print(dir(music)) at the end of the code-block I mentioned above.
What does buffering sounds even do you might ask?
It keeps the chosen sounds in your computer’s RAM, which helps less ‘performance-lag’ as Blender doesn’t need to keep re-importing the sound(s)
Hope it helps!