Audio stuttering

Godot Version

4.3

Question

im a new programmer and i wanted to add music that changes when an enemy chases you but when i load my code in it has a stuttering sound and never plays

func level_music():
	if Input.is_action_pressed("idle_act"):
		Global.idle = true
	if  Input.is_action_pressed("chase_acf"):
		Global.idle = false
	if Global.idle == true:
		$ent_player/Head/Camera3D/idle.playing = true
		print("idle play")
	elif Global.idle == false:
		$ent_player/Head/Camera3D/idle.playing = false
		print("chase play")

func _process(delta: float) -> void:
	level_music()

I suppose you’re using an AudioStreamPlayer.
According to the documentation of its playing property:

If true, this node is playing sounds. Setting this property has the same effect as play() and stop().

Since level_music() is called every frame, it seems you’re setting playing to true every frame, therefore calling play() every frame, which may explain the stuttering issue and sound not playing (actually it does, just resetting each frame).
I’m not sure because I’ve never ran into that issue, but that’s something you can explore?

Can you try something like this and see if it works?

var audioPlayer = $ent_player/Head/Camera3D/idle
if Global.idle == true:
    if not audioPlayer.player:
        audioPlayer.playing = true
        print("idle play")
elif Global.idle == false:
    if audioPlayer.player:
        audioPlayer.playing = false
        print("chase play")

Just checking if sound is not playing already before starting, and vice versa. I also added a audioPlayer variable to make the code more readable.

Let me know if that helps!

1 Like

This worked perfectly thank you!!!

1 Like