Overview of sound systems

There are two types of sounds available in the audio system: sound effects and music. Sound effects are short bursts of quick sounds that signal the player of the game making progress. A few examples of sound effects are gun noises, bullets firing, a character jumping, physics contact events and many others. Music is longer in length and usually played in a loop. A few examples of music are background music, cut scenes, successfully completing a milestone in the game and many others.

All audio assets are imported into the editor in the format of audioClip assets.

Music Playing

  1. Create an empty node on the Node Tree
  2. Select the empty node and click Add Component -> Components -> AudioSource at the bottom of the Properties to add the AudioSource component.
  3. Drag the audio resources in Assets to the Clip of the AudioSource component, as follows:

    audioclip

  4. Next, set the other parameter of the AudioSource component as needed, for the details please refer to the Audiosource Component documentation.

If you only need to play the audio automatically after the game is loaded, check the PlayOnAwake of the AudioSource component. If you want more flexibility in controlling AudioSource playback, you can get the AudioSource Component in the custom script and then call the appropriate API. As shown below:

  1. // AudioController.ts
  2. @ccclass("AudioController")
  3. export class AudioController extends Component {
  4. @property(AudioSource)
  5. public audioSource: AudioSource = null!;
  6. play () {
  7. this.audioSource.play();
  8. }
  9. pause () {
  10. this.audioSource.pause();
  11. }
  12. }

Next, add the corresponding custom component to the editor’s Properties. Selecting the corresponding node and add the custom component by clicking Add Component-> Custom script -> User Script at the bottom of the Properties. Then drag and drop the node with the AudioSource component onto Audio Source in the custom component. As shown below:

audiocontroller

Effect Playing

Compared to long music playback, audio effects playback has the following characteristics:

  • Short playback time
  • A large number of simultaneous playback

For such playback requirements, the AudioSource component provides the playOneShot interface to play audio effects. The specific code implementation is as follows:

  1. // AudioController.ts
  2. @ccclass("AudioController")
  3. export class AudioController extends Component {
  4. @property(AudioClip)
  5. public clip: AudioClip = null!;
  6. @property(AudioSource)
  7. public audioSource: AudioSource = null!;
  8. playOneShot () {
  9. this.audioSource.playOneShot(this.clip, 1);
  10. }
  11. }

Note: playOneShot is a one-time play operation, there is no way to pause or stop the audio after it is played, and no way to register the ended event callback.

Web Platform Playback Restrictions

Audio playback on the Web platform currently requires compliance with the latest Audio Play Police, and even if the AudioSource component is set to playOnAwake it will not start until the first user input is received. An example is as follows:

  1. // AudioController.ts
  2. @ccclass("AudioController")
  3. export class AudioController extends Component {
  4. @property(AudioSource)
  5. public audioSource: AudioSource = null!;
  6. start () {
  7. let btnNode = find('BUTTON_NODE_NAME');
  8. btnNode!.on(Node.EventType.TOUCH_START, this.playAudio, this);
  9. }
  10. playAudio () {
  11. this.audioSource.play();
  12. }
  13. }

Audio Asset AudioSource Component