AudioSource Component Reference

    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.

    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 interface for playing short sound effects, see the Sound Effect Playback section below for details.
    1. Add the AudioSource component to the node.

    2. audioClip

    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. ), then double-click to open the script for writing, as follows:

    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:

    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:

    Note: 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 documentation.

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