using a magic number for the tween interval duration was driving us nuts.
after a quick dive into the docs we found audiostreamplayer has a stream property and that stream property has a get_length method.
we replaced: tween.tween_interval(2.5)
with: tween.tween_interval(explosion_audio.stream.get_length())
I couldn’t find a great example for this in C# along the lines of await so here is how I did it using the GetLength like the original poster:
private double GetTransitionTime(AudioStreamPlayer audioStream)
{
// get the length of the audio for the transition time or the defaultTransitionTime if it is longer than the audio
double audioLength = audioStream.Stream.GetLength(); // Get the length of the audio
return audioLength > defaultTransitionTime ? audioLength : defaultTransitionTime;
}
private void CompleteLevel(String nextLevelFile)
{
GD.Print("Level Complete");
// get transition time based on the audio and start the audio
var transitionTime = GetTransitionTime(successAudio);
successAudio.Play();
// stop processing while transitioning
SetProcess(false);
isTransitioning = true;
// start the tween to wait before loading the next level
var tween = CreateTween();
tween.TweenCallback(Callable.From(() => GetTree().ChangeSceneToFile(nextLevelFile))).SetDelay(transitionTime);
}