AudioSource Component

audiosource

Properties

|Property | Description |

|:— | :— | |Clip | The audio resource object to be played |

|Loop | Whether to loop |

|PlayOnAwake | Whether to play audio automatically after the component is activated | |Volume | Audio volume, ranging from 0 to 1 |

Register AudioSource Event Callback

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

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

For more script interfaces for AudioSource, please refer to the AudioSource API. For specific playback controls, please refer to the Audio System Overview documentation.