How to add clips to an AudioStreamInteractive via GDScript?

Godot Version

4.3

Question

Hello, I have a large list of clips that I want to add to an AudioStreamInteractive. Because there are so many that will be generated at runtime I do not want to do it in the editor, and instead want them to be set up in code.

How can I add streams to both AudioStreamInteractive and AudioStreamSynchronized using GDScript? I haven’t had any luck in the docs or in forums.

Any help would be greatly appreciated!

Hi,
like so ?

extends AudioStreamPlayer

# the file names of the clips
var audio_files:Array[String] = [
	"clip1.mp3",
	"clip2.mp3",
	"clip3.mp3",
]
#to keep the references to the clips we just collect them into this array
var audio_clips:Array[AudioStream]

func _ready() -> void:
	#add a new interactive stream to this player
	stream = AudioStreamInteractive.new()
	
	#load all clips from the array of filepathes
	for path in audio_files:
		var file = FileAccess.open(path, FileAccess.READ)
		var sound = AudioStreamMP3.new()
		sound.data = file.get_buffer(file.get_length())
		audio_clips.append(sound)
		print("clip loaded "+path)
	
	#set the clip count
	stream.clip_count = audio_clips.size()
	
	#now add them to the stream
	for i in audio_clips.size():
		stream.set_clip_stream(i, audio_clips[i])
		stream.set_clip_name(i, audio_files[i])
		print("clip set "+audio_files[i]+" on index ", i)
	
	#add a transition to play
	stream.add_transition( 0, 1, 1, 0, AudioStreamInteractive.FadeMode.FADE_AUTOMATIC, 1)
	
	#lets go
	play()
1 Like

While reading your reply, all I could think was “How strange, this is exactly how I attempted it, why doesn’t mine work?”

That was until I realizes my error was just that I didn’t include the line:
stream = AudioStreamInteractive.new()

Thank you so much for your reply. This was incredibly helpful.