How to stop an audio stream when another stream is playing?

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

I have set a stage music but I have also set a audio stream in the character scene to play when the character dies. But whenever I try running both the sounds merge and sound awful. Is there any way to stop an audio stream of other scene when another stream is being played?

:bust_in_silhouette: Reply From: Somar

What I usually do is differentiate every type of audio. BGMs from SFX, etc… and I have a Singleton that I call passing two arguments, “path” that holds a string for the audio path and “type”, a string specifying the type of audio that is going to be played. The singleton instances an AudioStream scene with a Timer node, the path is add and play() is on _ready(). The AudioStream, then adds itself to the group(type). Here is where you can make the previous sound stop. On _ready() you could check for every node in the group(type) and queue_free, stop(), or pause, every one of them.

The Timer.wait_time() is set as the audio length time. When the timer ends, queue_free().

I am quiet new so can you explain it a bit more easily
In my case my background music is in my stage scene and my death sound is in my player scene. My Background is set to autoplay and my death sound is set to play in my player’s code when the player dies in the func _dead(). Now both the sounds are colliding with each other. This is the problem.

Nanogines | 2019-06-24 05:21

Sure can. We are going to create an automatic audio player. That not only erases itself after the audio has finished, but also stores the current playing audios in a group so that you can free then when necessary.

For this you will be using “groups”, and “singletons”.
SceneTree - group methods

You can make a scene that consists of only an AudioStreamPlayer and name the node Audio. Attach a script to it and only put in this:

func _on_Audio_finished(): #by the way this is a signal connection


The next step is creating an autoload script, that can be called from anywhere just by using it’s prefix. In this autoload script you are going to preload the previously created scene using:

var Audio = preload("res://Audio.tscn") 

Keep in mind that the path to the file may be different for your project, so just copy the path from your editor at the FileSystem’s dock.

Next create a function “_play(filepath, type)” these are going to receive the arguments filepath, and type. And then:

var audio = Audio.instance()
var file = load(filepath) 

Line by line:

  1. instances the Audio scene we created first
  2. adds the instance to the tree. (the path and the parent is your option)
  3. this adds the instance to a group using the passed argument “type”, it is important that “type” hold a String type value, e.g: “BGM” or “SFX”
  4. this loads the file using the passed argument “filepath” into the file variable. It is also important that “filepath” hold a String type value, e.g: “res://Audio/sword-attack.wav”
  5. We then set the just created variable file as current audiot to be played by the AudioStreamPlayer.
  6. And finally we play it using play()

Now, remember that the Audio has a script of it own that will queue_free() itself as soon as the audio comes to an end. But if you want to end it before, you can just use:

var currentPlayingAudio = get_tree().get_nodes_in_group("mygroupname") 

You wll use any group name you assigned before in the autoload script.
Then you can iterate through currentPlayingAudio and queue_free() all of then, or just select one or other to queue_free().

Well, that is it. Sorry if it is a little complicated. That was just the way I figured out how to do it.

Somar | 2019-06-24 16:47