2.4 KiB
Blending Music Between Scenes
This page covers the ProjectMusicController, which is used to blend music in between scenes, that would otherwise abruptly stop the music when they get unloaded.
Setup
-
Verify the
Musicaudio bus.- Open the Audio bus editor.
- Confirm that
Musicaudio bus is available.- If the last bus is
New Bus, try restarting the editor and checking again.
- If the last bus is
- If the audio bus doesn't exist, add it and save the project.
-
Verify the
ProjectMusicControllerautoload.- Open the Project Settings.
- Open the
Globalstab. - Confirm that the
ProjectMusicControlleris listed as an autoload.
-
Setup music blending between scenes.
- Open up the
project_music_controller.tscnscene. - Inspect the root node of the scene tree (
ProjectMusicController). - Confirm that the
audio_busis set toMusic. - Expand the
Blendingvariable group. - Set a
fade_out_durationorfade_in_duration, in seconds.
- Open up the
-
Add background music to your scenes.
- Import the music asset into the project.
- Add a
BackgroundMusicPlayer. - Assign the music asset to the
streamproperty. - Make sure that the
busproperty is set toMusicandautoplayistrue. - Save the scene.
Internal Details
When a background music player is about to exit the scene tree, it gets reparented to the autoload node. This allows it to continue playing until the next scene is ready and a new background music player is detected. If the audio stream players share the same stream, then the controller will seek the next player to the same position on the stream, before removing the previous one. If a different stream is detected and a fade out duration is set, then the previous player will blend into the next one, by having its volume lowered to zero over the fade out duration.
The autload adds the "BlendMusic" audio bus is added at runtime. If a fade in duration is set, then the temporary bus is used to combine the increasing volume of the player with any other animations local to the scene.
The autoload will work with any AudioStreamPlayer with bus set to Music and autoplay set to true. These are detected up as they enter the scene tree. To dynamically add an AudioStreamPlayer to the background music, call ProjectMusicController.play_stream(background_music_player).