Audio makes popping sound when using Input is action pressed

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

I am trying to make a sound happen when a button is pressed but for some reason it makes a popping sound. If i make the sound happen on ready func it will sound normal. The sound is mp3, how can i fix this?

:bust_in_silhouette: Reply From: BunnzoSteel

I had an issue where I was trying to play a sound when an object was destroyed but I couldn’t hear those sounds. The problem was that by calling queue_free to remove the object, the playing sound terminated instantly as well.

It looked like the way to solve this (your situation might be something similar) was to have an AudioStreamPlayer someplace else in the Scene Tree and send it signals. I was trying to plug this together, when I found this bit of code for an audio manager script: https://kidscancode.org/godot_recipes/audio/audio_manager/

extends Node

var num_players = 8
var bus = "master"

var available = []  # The available players.
var queue = []  # The queue of sounds to play.


func _ready():
    # Create the pool of AudioStreamPlayer nodes.
    for i in num_players:
        var p = AudioStreamPlayer.new()
        add_child(p)
        available.append(p)
        p.connect("finished", self, "_on_stream_finished", [p])
        p.bus = bus


func _on_stream_finished(stream):
    # When finished playing a stream, make the player available again.
    available.append(stream)


func play(sound_path):
    queue.append(sound_path)


func _process(delta):
	# Play a queued sound if any players are available.
    if not queue.empty() and not available.empty():
        available[0].stream = load(queue.pop_front())
        available[0].play()
        available.pop_front()

I named this script AudioManager and set it to autoload in Project Settings. Now I just need to "AudioManager.play(“res://sound.mp3”) from anywhere in the scene tree. This works very nicely and is how I plan to set up all my Godot audio from now on. You might want to try it because it sounds like something might be terminating your sound prematurely.

Another option is spawning StreamPlayers without pooling them. When a (oneshot) sfx is needed, simply create a new StreamPlayer and connect its "finished" signal to its queue_free method, so it self-destructs.
You might want to also add them to an array in case you need to stop/free all the audio at once, for example when changing scenes.

bloqm | 2022-01-03 22:07