Super weird error: sound

it happens all the time with any project that includes audio.

if the laptop is plugged in, the sound plays. if the laptop is not plugged in, the sound does not play.

also, the game in progress in the mobile browser freezes immediately after the sound is triggered but you can’t hear any noise.

you can make a button-triggered sound to test it. if the sound starts by itself, the game will not start anyway.

platform: windows, android.
browser: it doesn’t matter.

platform: android
browser: it doesn’t matter.

export as: html game.

thanks for read.

I need to add:

audio works when running mobile browser in “desktop” mode.

my system:

i7 13th gen intel
64gb ram
rtx 4060
win 10

Hi, could you please elaborate on what you mean by “is plugged in”? Does this refer to the power cable? Or to headphones?

Also, how do you play the sound? Logic nodes? Haxe? Can you please either share a screenshot of your logic tree or share the relevant part of the code?

What mobile browser do you use? Does it matter which mobile browser you use or does the issue happen with one specific browser only? If possible, please share a small example project.

if the sound starts by itself, the game will not start anyway.

What do you mean by that?

plugged in mean, power cable. (for laptop)

added UI canvas button. on canvas element node output connected to “play sound” node.

added speker and load16 bit 44100 wav sound (any long)

just touch or click button and it will happens.

“What mobile browser do you use?”

I tried with chrome, firefox and opera with android and tried edge, opera and firefox on laptop. same problem.

“What do you mean by that?”

if game starts with sound automatically (after game loads with “on init” node)

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.