Non-spatialized audio

BeginnerProgrammer Non-spatialized audio sounds the same throughout the scene, regardless of the position of entities (such as the player camera). It’s stereo and moves along a single axis (usually the X-axis). Unlike spatialized audio, the volume, pitch (frequency), and other parameters of spatialized audio don’t change. This is useful, for example, for background music and menu sound effects. audio emitters or audio listeners.

1. Import audio and include it in the build

  • Import the audio as a audio asset.
  • root asset. Root assets are assets that Xenko includes in the build so they can be used at runtime. Asset View, right-click the asset and select Include in build as root asset: Do not include in build as root asset, the option is already selected and you don’t need to change it.

    2. Create a script to play audio

    To play non-spatialized audio at runtime, create an instance of it and define its behavior in the code. SoundInstance controls audio at runtime with the following properties: SoundInstance API documentation.
    Note
    SoundInstance.Play.The same goes for SoundInstance.Pause (when a sound is already paused) and SoundInstance.Stop (when a sound is already stopped). For example, the following code:
  • -
    1. public override async Task Execute(){ // Load the sound Sound musicSound = Content.Load<Sound>("MySound"); // Create a sound instance SoundInstance music = musicSound.CreateInstance(); // Loop music.IsLooping = true; // Set the volume music.Volume = 0.25f; // Play the music music.Play();}

    Alternative: create a script with public variables

    Create a public variable for each audio asset you want to use. You can use the same properties listed above. For example:
    1. public class MySoundScript : SyncScript{ public Sound MyMusic; private SoundInstance musicInstance; public bool PlayMusic; public override void Start() { musicInstance = MyMusic.CreateInstance(); } public override void Update() { // If music isn't playing but should be, play the music. if (PlayMusic & musicInstance.PlayState != SoundPlayState.Playing) { musicInstance.Play(); } // If music is playing but shouldn't be, stop the music. else if (!PlayMusic) { musicInstance.Stop(); } }}

    Add the script to the entity

  • Scene view, select the entity you want to add the script to:
  • Property Grid, click Add component and select your script: The script is added to the entity.
  • public variables to the script, you need to tie them to audio assets. Asset View to each variable: (Select an asset): Then choose the audio asset you want to use:

    See also

  • Import audio
  • Global audio settings
  • Spatialized audio