Super weird error: sound

Hi, sorry for my late reply. I now tested your issue on my laptop and I think I know what the problem is. Actually, this issue already came up earlier this year on Discord.

On some targets, Kha loads sounds asynchronously. The Play Sound node works quite unintuitive in this case: If the sound is not loaded already, the node instructs Kha to load the sound and then doesn’t wait for the sound to be loaded. If you later execute the node again when the sound is already loaded, it will play the sound.

I guess that loading a sound is a bit faster if you have plugged in a power cable (= usually higher CPU frequency, e.g.), and since speaker objects instruct Kha to load their sound once the game starts I think that in your case the sound is already loaded when the power cable is plugged in and you press the button, and if the cable is not connected it takes a bit longer and you press the button before the sound is fully loaded. If you click on the button that plays the sound a few times, the sound should eventually also start playing without being connected to a power cable.

Additionally, loading sounds in the browser is quite slow in Kha, since if the sounds are compressed Kha splits decompression over many frames (html5 target only). It’s pretty annoying, and this might be what causes your mobile browser(s) to freeze. On the other hand, you mentioned that it works when running in “desktop” mode, so maybe the freezes have another reason after all… I haven’t tested this specific issue yet, so I can only speculate.

So ultimately, we need a redesign of how the Play Sound node works… I started to work on it a while ago, but I didn’t continue because there were some open questions on how exactly it should behave (there are multiple different use cases) and maybe there were some other problems as well. I should probably revisit this someday…

If you know Haxe, you can workaround all this by just playing the sound once it’s loaded. If not, you can display a loading screen, play the sound with volume 0 and wait for the Is Running output of the sound node to be activated. Then the sound is loaded and you can start the actual game.

3 Likes