Non-spatialized audio

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 requires no or audio listeners.

  • .

  • 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

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:

For more details, see the .

Note

If the sound is already playing, Xenko ignores all additional calls to SoundInstance.Play.The same goes for (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

Create a public variable for each audio asset you want to use. You can use the same properties listed above.

For example:

  1. {
  2. private SoundInstance musicInstance;
  3. public bool PlayMusic;
  4. public override void Start()
  5. {
  6. }
  7. public override void Update()
  8. {
  9. // If music isn't playing but should be, play the music.
  10. if (PlayMusic & musicInstance.PlayState != SoundPlayState.Playing)
  11. {
  12. // If music is playing but shouldn't be, stop the music.
  13. else if (!PlayMusic)
  14. {
  15. musicInstance.Stop();
  16. }
  17. }
  18. }
  • In the Scene view, select the entity you want to add the script to:
  • In the Property Grid, click Add component and select your script:

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 (Select an asset):

Pick up an asset