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.

Non-spatialized audio

Non-spatialized audio requires no audio emitters or audio listeners.

1. Import audio and include it in the build

  • Import the audio as a audio asset.

  • Make sure the audio asset is a root asset. Root assets are assets that Xenko includes in the build so they can be used at runtime.

    In the Asset View, right-click the asset and select Include in build as root asset:

Include in build as root asset

If the menu option reads 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.

The SoundInstance controls audio at runtime with the following properties:

PropertyFunction
IsLoopingGets or sets looping of the audio.
PanSets the balance between left and right speakers. By default, each speaker a value of 0.5.
PitchGets or sets the audio pitch (frequency).
PlayStateGets the state of the SoundInstance.
PositionGets the current play position of the audio.
VolumeSets the audio volume.

For more details, see the SoundInstance API documentation.

Note

If the sound is already playing, Xenko ignores all additional calls to 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:

  • instantiates non-spatialized audio
  • sets the audio to loop
  • sets the volume
  • plays the audio
  1. public override async Task Execute()
  2. {
  3. // Load the sound
  4. Sound musicSound = Content.Load<Sound>("MySound");
  5. // Create a sound instance
  6. SoundInstance music = musicSound.CreateInstance();
  7. // Loop
  8. music.IsLooping = true;
  9. // Set the volume
  10. music.Volume = 0.25f;
  11. // Play the music
  12. music.Play();
  13. }

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
  2. {
  3. public Sound MyMusic;
  4. private SoundInstance musicInstance;
  5. public bool PlayMusic;
  6. public override void Start()
  7. {
  8. musicInstance = MyMusic.CreateInstance();
  9. }
  10. public override void Update()
  11. {
  12. // If music isn't playing but should be, play the music.
  13. if (PlayMusic & musicInstance.PlayState != SoundPlayState.Playing)
  14. {
  15. musicInstance.Play();
  16. }
  17. // If music is playing but shouldn't be, stop the music.
  18. else if (!PlayMusic)
  19. {
  20. musicInstance.Stop();
  21. }
  22. }
  23. }

Add the script to the entity

  • In the Scene view, select the entity you want to add the script to:

Select an entity

  • In the Property Grid, click Add component and select your script:

Click Add component

The script is added to the entity.

  • If you added public variables to the script, you need to tie them to audio assets.

    Drag and drop an asset from the Asset View to each variable:

Drag and drop an audio asset

Alternatively, click Hand icon (Select an asset):

Pick up an asset

Then choose the audio asset you want to use:

Select an audio asset

See also