AudioSource Component Reference

The AudioSource component is used to control the playback of music and sound effects.

audioSource

Select the node in the Hierarchy panel, then click the Add Component button at the bottom of the Inspector panel and select Audio -> AudioSource to add the AudioSource component to the node.

AudioSource Properties

PropertyDescription
ClipThe added audio asset for playback , default is empty, click the arrow button behind it to select.
LoopWhether to loop.
PlayOnAwakeWhether the audio will be played automatically when the game is running (component is active).
VolumeVolume, in the range 0~1.

Audio Playback

Cocos Creator 3.x uses AudioSource to control the playback of audio, which is a component that can be added to the scene, set by the Editor, or called in a script.

In addition, Creator divides audio into longer music and shorter sound effects based on their length.

  • If you control audio playback through the editor, there is no difference between playing music and sound effects, but long music is recommended. See the Playback via Editor section below for details.
  • If audio playback is controlled via script, the AudioSource component additionally provides the playOneShot interface for playing short sound effects, see the Sound Effect Playback section below for details.

Note: Cocos Creator 3.x removes the audioEngine API from v2.x and uses the AudioSource component for audio playback.

Via the editor

  1. Add the AudioSource component to the node.

  2. Drag and drop the required audio asset from the Assets panel into the Clip property box of the AudioSource component as follows:

    audioClip

  3. Set the other properties of the AudioSource component as needed.

Via script

For more flexible control of AudioSource playback, you can add a custom script to the node where the AudioSource component is located and then call the appropriate API to control audio playback.

  1. Add the AudioSource component to the node and specify the audio asset.

  2. Create script in the Assets panel and name it (e.g. AudioController), then double-click to open the script for writing, as follows:

    1. import { _decorator, Component, Node, AudioSource, assert } from 'cc';
    2. const { ccclass, property } = _decorator;
    3. @ccclass("AudioController")
    4. export class AudioController extends Component {
    5. @property(AudioSource)
    6. public _audioSource: AudioSource = null!
    7. onLoad () {
    8. // Get the AudioSource component
    9. const audioSource = this.node.getComponent(AudioSource)! ;
    10. // Check if it contains AudioSource, if not, output an error message
    11. assert(audioSource);
    12. // Assign the component to the global variable _audioSource
    13. this._audioSource = audioSource;
    14. }
    15. play () {
    16. // Play the music
    17. this._audioSource.play();
    18. }
    19. pause () {
    20. // Pause the music
    21. this._audioSource.pause();
    22. }
    23. }
  3. Select the node in the Hierarchy panel, then drag and drop the script from the Assets panel to the Inspector panel to add the script component to the node. As shown below:

    audioSource

Audio Playback

Compared to long music playback, sound effect playback has the following characteristics:

  • Short playback time
  • Large number of simultaneous playback

The AudioSource component provides the playOneShot interface to play sound effects. The specific code implementation is as follows:

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

Note: playOneShot is a one-time play operation, the sound after playing cannot be paused or stopped, and cannot listen to the end-of-play event callback.

For more audio-related scripting interfaces, please refer to AudioSource API.

For more control over audio playback, please refer to the AudioSource playback example documentation.

Register AudioSource Event Callback

Cocos Creator supports registering event callbacks on AudioSource components, with the following usage examples:

  1. import { _decorator, Component, Node, AudioSource } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. @ccclass('AudioDemo')
  4. export class AudioDemo extends Component {
  5. @property(AudioSource)
  6. audioSource: AudioSource = null!;
  7. onEnable () {
  8. // Register the started event callback
  9. this.audioSource.node.on(AudioSource.EventType.STARTED, this.onAudioStarted, this);
  10. // Register the ended event callback
  11. this.audioSource.node.on(AudioSource.EventType.ENDED, this.onAudioEnded, this);
  12. }
  13. onDisable () {
  14. this.audioSource.node.off(AudioSource.EventType.STARTED, this.onAudioStarted, this);
  15. this.audioSource.node.off(AudioSource.EventType.ENDED, this.onAudioEnded, this);
  16. }
  17. onAudioStarted () {
  18. // TODO...
  19. }
  20. onAudioEnded () {
  21. // TODO...
  22. }
  23. }

Web platform playback restrictions

Audio playback on the Web platform currently requires compliance with the latest Audio Play Policy, and even if the AudioSource component has playOnAwake set, It also needs to be played after the first user click event occurs.