Significant audio delay on some Android devices

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By sander48k

I am creating a 3D game with Godot 3.5.2 for mobile devices, both iOS and Android. Today I noticed that on some Android devices there is significant delay between a UI action, e.g. a button press, and the playback of a sound. I am using AudioStreamPlayer. The playback is started from GDScript when some action occurs.

To isolate the issue I created a very simple Godot project consisting of 1 scene that contains a Button and an AudioStreamPlayer. The ‘pressed’ signal triggers the following function:

func _on_button_pressed():
    var time_ttnm = AudioServer.get_time_to_next_mix()
    var time_latency = AudioServer.get_output_latency()
	
    print('ttnm=%f, latency=%f' % [time_ttnm, time_latency])
    $AudioStreamPlayer.play()

On iOS devices and most Android devices I have there is no noticeable delay. On one Android device however (Motorola e40 running Android 11) there is a significant and unacceptable delay (roughly half a second) between the button press and the playback of the sound.

Other info:

  • The AudioStreamPlayer uses a very short sample (150ms) in WAV format.
  • The time to next mix is around 0.01. The latency is 0.
  • I also tested this in Godot 4.0.3 which gave the same result.

Anyone any idea what is going on here and how it can be fixed?

Looks like a platform-specific problem. Perhaps you should report this on the issue tracker on Github?
You could also try using Godot 4 to see if the issue persists (using a low end renderer if you need compatibility)

Zylann | 2023-07-06 14:48

I’d try different file formats at different sampling rates first. Maybe devices decoding facilities stumble on some odd files. Look up “android supported media formats”. Though the engine should probably already encode audio resources to the target platforms requirements…

Rpahut | 2023-07-07 12:54

:bust_in_silhouette: Reply From: DaWi

Hi,

I am new to GODOT but I did run into this exact issue a few years ago, new Android devices were ok but older ones had very bad audio latency.
I was making a gravity game where you pilot a small triangle around the screen using thrust and left/right on-screen control ‘buttons’. I wanted the thrust noise to be immediate or it just wasn’t worth doing.
The only way I solved it was to make a short (but perfectly looping) white noise WAV and have it’s volume set to 0 by default. Setting the volume of an already playing sound is instant so, when the user touched the on screen boost button, the volume was set to 100, when let go, set back to 0. This method would also work to add tones (instead of white noise) to buttons. You could also add a small routine to fade the loop in or out quickly to get a ‘blip’ sound effect on the tones.

I hope this helps.