Play multiple sounds in quick succession

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

How do i play multiple sounds in quick succession without one overlapping the other?
i have a global sfx player but it has only 1 AudioStreamPlayer. I searched the internet and found polyphony but it is only for godot 2. is there something similar to polyphony for godot 3?

:bust_in_silhouette: Reply From: GameVisitor

One way of doing it would be to add a [signal][1] for the finished event, ex:

func _on_AudioStreamPlayer_finished():
    print( "Audio finished, start the next one logic")
	#your logic to play the next one or a random one here...
	pass

Hope this helps :slight_smile:

:bust_in_silhouette: Reply From: twinpixel

To enable polyphony for AudioStreamPlayers I have added the following script:

    #helper for multiple audio stream playback
    extends AudioStreamPlayer2D
    
   func play( from_position=0.0 ):
    	if !playing:
     		.play(from_position)
     	else:
     		var asp = self.duplicate(DUPLICATE_USE_INSTANCING)
      		get_parent().add_child(asp)
      		asp.stream = stream
      		asp.play()
      		yield(asp, "finished")
      		asp.queue_free()

It uses polymorphism to override the AudioStreams play()-function and instantiate a new player. This also works for audio tracks in the AnimationPlayer.